gpt4 book ai didi

ruby-on-rails-3 - Rails 3 - 搜查 - check_box_tag

转载 作者:行者123 更新时间:2023-12-04 19:58:04 26 4
gpt4 key购买 nike

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:

只是不确定如何实现它以接受多个复选框值。

最佳答案

不要使用“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 %>

关于ruby-on-rails-3 - Rails 3 - 搜查 - check_box_tag,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14394499/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com