【问题标题】:Simple form multiple select with checkboxes带有复选框的简单表单多选
【发布时间】:2014-04-21 18:40:07
【问题描述】:

我一直在尝试构建一个搜索表单,允许用户通过选择下拉列表中的多个复选框选择进行搜索。问题是,当我提交表单时,这些值在最后一个空值的数组中传递,当我检查日志时,查询看起来像这样

 UPDATE `searches` SET `ethnicity` = '---\n- Pacific Islander\n- White\n- Other\n- \'\'\n', `updated_at` = '2014-04-21 18:24:03' WHERE `searches`.`id` = 1

我不明白为什么我在每个表单值周围都有多余的字符。我正在使用简单表单,我的表单看起来像这样

<%= simple_form_for(:search, :url => {:controller => 'searches', :action => 'update', :slug => session[:username]}) do |f| %>
                <%= f.error_notification %>
<%= f.input :ethnicity, :label => "Ethnicity", :collection => ["Asian","Black", "Hispanic/Latino", "Indian", "Middle Eastern", "Native American", "Pacific Islander", "White", "Other"], :include_blank => "Anything", wrapper_html: { class: 'form-group' }, :as => :check_boxes, :input_html => {:name => "search[ethnicity][]", :multiple => true}, include_hidden: false %>

<% end %>

根据我的理解,我的搜索控制器允许一个数组,并且由于我使用 Rails 4,我使用强参数

def search_params
  params.require(:search).permit(:id, :user_id, :ethnicity => []) if params[:search]
end

我只是发布了不起作用的代码部分,因为我的表单还有其他字段,例如性别、身高、年龄等,但这些都可以正常工作。我只有在使用复选框时才会遇到问题。单选按钮和选择字段完美无缺。

这是提交时的完整搜索日志

Parameters: {"utf8"=>"✓",     "authenticity_token"=>"WMR/U8ztPOb+Y8vGoAcP2Le5k8T5ZC02r0uoVdJMuSg=", "search"=>{"gender"=>"Female", "interested_in"=>"Men", "min_age"=>"22", 
"max_age"=>"44", "ethnicity"=>["Pacific Islander", "White", "Other", ""], 
"education"=>"", "income"=>"", "religion"=>"", "body_type"=>"", "min_height"=>"", "max_height"=>"", "smoke"=>"", "drink"=>"", "drugs"=>"", "exercise"=>"", "have_children"=>"",
 "want_children"=>"", "pets"=>""}, "commit"=>"Search", "slug"=>"wahidp"}
Geokit is using the domain: localhost
User Load (0.5ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1

Search Load (0.3ms)  SELECT `searches`.* FROM `searches` WHERE `searches`.`user_id` = 1 LIMIT 1
   (0.1ms)  BEGIN
  SQL (0.4ms)  UPDATE `searches` SET `ethnicity` = '---\n- Pacific Islander\n- White\n- 
   Other\n- \'\'\n', `updated_at` = '2014-04-21 18:24:03' WHERE `searches`.`id` = 1

我完全是一个初学者,可能正在构建一些超出我的范围的东西,但我已经彻底研究了这个问题并且需要更多帮助。谁能解释为什么这不起作用以及我需要做什么?谢谢!

我尝试删除 :include_blank => "Anything" 但我收到了相同的日志输出

Processing by SearchesController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"WMR/U8ztPOb+Y8vGoAcP2Le5k8T5ZC02r0uoVdJMuSg=", "search"=>{"gender"=>"Female", "interested_in"=>"Men", "min_age"=>"22", "max_age"=>"44", "ethnicity"=>["Other", ""], "education"=>"", "income"=>"", "religion"=>"", "body_type"=>"", "min_height"=>"", "max_height"=>"", "smoke"=>"", "drink"=>"", "drugs"=>"", "exercise"=>"", "have_children"=>"", "want_children"=>"", "pets"=>""}, "commit"=>"Search", "slug"=>"wahidp"}
Geokit is using the domain: localhost
User Load (0.6ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
Search Load (0.4ms)  SELECT `searches`.* FROM `searches` WHERE `searches`.`user_id` = 1 LIMIT 1
  (0.1ms)  BEGIN
 SQL (0.3ms)  UPDATE `searches` SET `ethnicity` = '---\n- Other\n- \'\'\n', `updated_at` = '2014-04-21 21:24:55' WHERE `searches`.`id` = 1 

【问题讨论】:

    标签: mysql ruby-on-rails forms simple-form


    【解决方案1】:

    按照以下建议更新checkboxes 代码:

    <%= f.input :ethnicity, :label => "Ethnicity", :collection => ["Asian","Black", "Hispanic/Latino", "Indian", "Middle Eastern", "Native American", "Pacific Islander", "White", "Other"], :include_blank => "Anything", wrapper_html: { class: 'form-group' }, :as => :check_boxes, include_hidden: false, :input_html => {:name => "search[ethnicity][]", :multiple => true} %>
    

    更新

    updating 记录之前将以下内容添加到SearchesController#update 操作。

    ## This will remove the extra "" value from  params[:search][:ethnicity]
    params[:search][:ethnicity] = params[:search][:ethnicity].reject(&:empty?) 
    

    我做了将近 2 小时的研究,只是为了找出为什么 include_hidden: false 不能按预期工作,并发现在 Rails 4 发布之前出现了一些问题。但即使在 Rails 4 发布后,这个问题仍然存在。这就是为什么上述替代方案暂时可行的原因。

    【讨论】:

    • 我删除了 :include_blank => "Anything" 选项,但仍然没有运气。
    • 你能用搜索时生成的新服务器日志更新问题吗?
    • 好的,我做到了。日志产生了相同的输出。
    • 您使用的 Rails 版本是什么?
    • @wahidp 抱歉,更改了代码。使用更新的。
    【解决方案2】:

    经过几天的研究,我终于找到了答案。首先我必须添加

     serialize :ethnicity, Array
    

    到搜索模型的顶部。然后在同一模型中,在跟随 Ryan Bates 高级搜索视频时,我将查询更正为如下所示

    users = users.where("ethnicity in (?)", ethnicity) if ethnicity.present?
    

    感谢 Kirti Thorat 的所有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 2015-04-14
      • 1970-01-01
      • 1970-01-01
      • 2021-06-25
      • 1970-01-01
      相关资源
      最近更新 更多