2013-01-11 82 views
0

Index操作改變元搜索參數

@search = Project.search(params[:search]) 
@projects = @search.order('created_at desc').paginate(:page => params[:page], :per_page => 100) 
@total_projects = Project.count 
respond_to do |format| 
    format.html 
    format.mobile 
    format.json { render :json => @projects } 
end  

如果params[:search][:stage_in]存在,並以「TIME_END」我想改變它像這樣params[:search][:stage_in] == "started"這是平等的,它可以與@search

回答

0

我會做一個工作單獨的方法爲你做這個只是爲了清潔。還有最後一部分是否爲'=='?:

@search = Project.search(search_params(params)) 
@projects = @search.order('created_at desc').paginate(:page => params[:page], :per_page => 100) 
@total_projects = Project.count 
respond_to do |format| 
    format.html 
    format.mobile 
    format.json { render :json => @projects } 
end 

def search_params(params) 
    params[:search][:stage_in] = "started" if params[:search][:stage_in] == "time_end" 
    params 
end