Listing Model - belongs_to :area
Area Model - has_many :listings
我试图实现搜索使用Ransack与复选框;当用户检查所选区域时,search返回所选区域的所有列表。
<%= 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:
只是不确定如何实现它来接受多个复选框值
用"area_id_any"代替"area_id_eq"。您还需要检查以确保您的参数被选中:
<%= 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 %>