mysql - 带复选框的简单表单多选

标签 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 %>

根据我的理解,我的搜索 Controller 允许使用数组,因为我使用的是 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 

最佳答案

按照下面的建议更新 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 操作中。

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

关于mysql - 带复选框的简单表单多选,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23203623/

相关文章:

mysql - 关键字 'CONSTRAINT' 附近的语法不正确

ruby-on-rails - strftime - 是否修改该值?

ruby-on-rails - 递归/树状强参数?

ruby-on-rails - 在 Ruby on Rails 上通过 form_tag 表单上传文件

php - 使用 PHP $_POST 记住选择框中的选项?

php - 如何刷新表格并在插入表格行时发出警报声?

mysql - mysql从多个表中获取数据,逗号分隔

php - Codeigniter 3 - 连接不返回所有结果

ruby-on-rails - rspec-sidekiq:如何用另一个类方法测试 "within_sidekiq_retries_exhausted_block"

javascript - 在一页上使用具有多个表单的日期选择器