mercredi 20 avril 2016

Smplifying my nested if statement

I have trouble in building nested if statements. I have an index page in which I show records based on some condition. My controller code for that is,

def index_orderSummary
  if Date.today.month>=4
    @order_summary = OrderSummary.where(:created_at=>("#{Time.now.year}-04-01")..("#{Time.now.year+1}-03-31"))
  else
    @order_summary = OrderSummary.where(:created_at=>("#{Time.now.year-1}-04-01")..("#{Time.now.year}-03-31"))
  end
  @order_summary = @order_summary.paginate(page: params[:page]).per_page(10).order("order_no ASC")
end

def index_issues
    @user = User.new
    @user = User.find(session[:user_id]).name
    if Date.today.month>=4
      @issue = Issue.where(:created_at=>("#{Time.now.year}-04-01")..("#{Time.now.year+1}-03-31"))
    else
      @issue = Issue.where(:created_at=>("#{Time.now.year-1}-04-01")..("#{Time.now.year}-03-31"))
    end
    @issue = @issue.paginate(page: params[:page], per_page: 10).order("issue_slip_no ASC")
  end

I have used a search box for which I have written the following method in the controller.

def search
  if params[:id] == "search-order" then
    @order_summary = OrderSummary.where(:created_at=>("#{Time.now.year}-04-01")..("#{Time.now.year+1}-03-31")).search(params[:search])
    @order_summary = @order_summary.paginate(page: params[:page]).per_page(5).order('order_no ASC')
    render :action => :index_orderSummary
  elsif params[:id] == "search-issue" then
    @issue = Issue.where(:created_at=>("#{Time.now.year}-04-01")..("#{Time.now.year+1}-03-31")).search(params[:search])
    @issue = @issue.paginate(page: params[:page]).per_page(10).order("issue_slip_no ASC")
    render :action => :index_issues
  end
end

now what all I need is, I want to integrate both my search and index methods.

My index methods already having an if else statement., now i want to check if params comes from search then I want to display the searched results in the index. How do I achieve that? Pls intimate, if my question is not clear..

Kindly please help and correct me. Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire