1

使用 Ransack gem,我能够使用复选框对某些值进行搜索。但是,这不适用于多个复选框。我错过了什么?

看法:

<%= search_form_for @search do |f| %>
  Part Time:<%= check_box_tag "q[job_type_cont]", value = "Part time" %></br>
  Full Time:<%= check_box_tag "q[job_type_cont]", value = "Full time" %></br>
  <%= f.submit "Search" %>
<% end %>

索引操作:

def index
  @search = Job.search(params[:q])
  @jobs = @search.result
 end
4

2 回答 2

2

您的视图将始终在params[:q][:job_type_cont]中仅发送一个值, 它将是兼职全职的字符串 对于接收多个值,您需要将其更改为数组(您需要更改复选框名称属性)如下

Part Time: <%= check_box_tag "q[job_type_cont][]", "Part time" %></br>
Full Time: <%= check_box_tag "q[job_type_cont][]", "Full time" %>

通过上述实现,在控制器中params[:q][:job_type_cont]将是一个数组,其中包含从视图中选择的值

于 2013-11-07T09:05:44.807 回答
2

卡尔佩什建议

Part Time: <%= check_box_tag "q[job_type_cont][]", "Part time" %></br>
Full Time: <%= check_box_tag "q[job_type_cont][]", "Full time" %>

@search = Job.search(job_type_in: params[:q][:job_type_cont])
@jobs = @ search.result

这实际上是一种迂回的说法:

Part Time: <%= check_box_tag "q[job_type_in][]", "Part time" %></br>
Full Time: <%= check_box_tag "q[job_type_in][]", "Full time" %>

这样你的控制器就不需要有趣的事情了:

@search = Job.search(params[:q])
于 2014-01-16T22:12:36.413 回答