简单形式多选复选框

时间:2014-04-21 18:40:08

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

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

 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 =&gt; &#34;任何&#34;但我收到了相同的日志输出

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 

2 个答案:

答案 0 :(得分:5)

按照以下建议更新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} %>

<强>更新

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

## 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发布之后,这个问题仍然存在。这就是为什么上述替代方案暂时有效。

答案 1 :(得分:0)

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

 serialize :ethnicity, Array

到搜索模型的顶部。然后在同一模型中,在关注Ryan Bates高级搜索视频的同时,我将查询更正为

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

感谢Kirti Thorat的所有帮助!