Ruby on rails 搜索包括参数
我在用gem的“搜索”来搜索记录 我正在编程的页面开始时只有一个下拉菜单来选择Ruby on rails 搜索包括参数,ruby-on-rails,ransack,Ruby On Rails,Ransack,我在用gem的“搜索”来搜索记录 我正在编程的页面开始时只有一个下拉菜单来选择@department。 在那之后,我想在@部门内搜索 我需要包括一个额外的参数department=2,以及搜索参数。(如果我将其插入浏览器URL中,它就会工作) 输入部门后的浏览器URL=2 http://localhost:5000/departments/costfuture?department=2&utf8=%E2%9C%93&q%5Bid_eq%5D=&q%5Bproject_ye
@department
。
在那之后,我想在@部门内搜索
我需要包括一个额外的参数department=2
,以及搜索参数。(如果我将其插入浏览器URL中,它就会工作)
输入部门后的浏览器URL=2
http://localhost:5000/departments/costfuture?department=2&utf8=%E2%9C%93&q%5Bid_eq%5D=&q%5Bproject_year_eq%5D=&q%5Boriginal_year_eq%5D=&q%5Bclient_id_eq%5D=43&q%5Blocation_name_cont%5D=&q%5Bproject_name_cont%5D=&q%5Bcoststatus_id_in%5D%5B%5D=&q%5Bcoststatus_id_not_in%5D%5B%5D=&q%5Brebudget_true%5D=0&q%5Bnew_true%5D=0&q%5Bconstruction_true%5D=0&q%5Bmaintenance_true%5D=0&commit=Search
这是控制器:
def costfuture
@departments = current_user.contact.departments
if params[:department]
@department = Department.find(params[:department])
@search = @department.costprojects.future.search(params[:q])
@costprojects = @search.result.order(:department_priority)
end
respond_to do |format|
format.html # index.html.erb
format.json { render json: @departments }
end
end
我试过这样的观点:
<%= search_form_for @search, url: departments_costfuture_path(:department => @department.id) do |f| %>
@department.id)do | f |%>
但是,生成的URL缺少department=2
如果要传递department
选项进行搜索,可以在搜索表单中使用隐藏字段进行搜索:
<%= search_form_for @search, url: departments_costfuture_path do |f| %>
<%= f.hidden_field :department_eq, value: @department.id %>
<%# Other form code here %>
<% end %>
现在,您可以使用生成的URL帮助器生成将设置参数[:id]
的链接,并可以使用该链接检索@department
我最后使用了隐藏字段标签在搜索表单中查找
<%= search_form_for @search, url: departments_costfuture_path do |f| %>
<%= hidden_field_tag :department, @department.id %>
<%= f.search_field :last_name_or_first_name_cont %>
<% end %>
然后,可以在控制器中访问params[:department]
和params[:search]
。谢谢您的帮助-但是,这一行给了我“未定义的方法'merge'for 2:Fixnum”,在没有看到完整错误和控制器操作(可能还有您的模型定义)的情况下,很难找到它。请将这些添加到您的问题中,好吗?这在视图@department.id%>
中起作用。但是,我需要从参数[:q]
中提取department=x。你知道怎么做吗?这在控制器中起了作用-获得隐藏的department_eq-`@department=params[:q][:department_eq]`谢谢更新-我在回答中添加了更多细节。:)
<%= search_form_for @search, url: departments_costfuture_path do |f| %>
<%= hidden_field_tag :department, @department.id %>
<%= f.search_field :last_name_or_first_name_cont %>
<% end %>