Listing Model - belongs_to :area
Area Model - has_many :listings
我正在尝试使用带有复选框的Ransack实现搜索;在用户检查所选区域的情况下,搜索将返回所选区域的所有列表。
<%= search_form_for @search do |f| %>
<% areas = Area.all %>
<% areas.each do |area| %>
<%= check_box_tag('q[area_id_eq][]', area.id) %>
<%= area.location%>
<% end%>
<%= f.submit "SEARCH" %>
<% end %>
控制台输出:
Parameters: {"utf8"=>"✓", "q"=>{"area_id_eq"=>["1", "2"]}, "commit"=>"SEARCH"}
Completed 500 Internal Server Error in 4ms
NoMethodError - undefined method `to_i' for ["1", "2"]:Array:
只是不确定如何实现它以接受多个复选框值。
答案 0 :(得分:5)
不使用“area_id_eq”,而是使用“area_id_any”。您还需要检查以确保选择了参数:
<%= search_form_for @search do |f| %>
<% areas = Area.all %>
<% areas.each do |area| %>
<%= check_box_tag('q[area_id_eq_any][]', area.id, (params[:q][area_id_eq_any].include? area.id.to_s) ? true : false ) %>
<%= area.location%>
<% end%>
<%= f.submit "SEARCH" %>
<% end %>