Question

I've been trying to build a search form that allows users to search by selection multiple checkbox selections in a drop down list. The problem that when I submit the form, the values are getting passed in an array with one empty value at the end and when I check the logs the query looks like this

 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

I dont understand why I have the extra characters around each form value. I am using Simple Form and my form look like this

<%= 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 %>

My search controller allows an array from my understanding and since Im using Rails 4, Im using strong parameters

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

Im just posting the section of the code that is not working as my form has other fields such as gender, height, age, etc but those all work fine. I'm only having problems when I use checkboxes. Radio buttons and select fields work perfect.

Here is a whole search log when submitted

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

Im totally a beginner that is probably building something way out of my league but I've researched this problem thoroughly and need more help. Can anyone explain why this isnt working and what I need to do? Thanks!

I tried removing :include_blank => "Anything" but I received the same log output

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 
Was it helpful?

Solution 2

After a couple more days of researching I finally figured out the answer. First I had to add

 serialize :ethnicity, Array

to the top of the search model. Then in the same model, while following Ryan Bates advanced search video, I corrected my query to look like this

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

Thanks Kirti Thorat for all your help!

OTHER TIPS

Update the checkboxes code as suggested below:

<%= 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} %>

UPDATE

Add the following to SearchesController#update action BEFORE updating the record.

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

I did almost 2 hrs of research just to find out why include_hidden: false would not work as expected and found out that there were some issues raised before Rails 4 release. BUT even after Rails 4 release this issue still continues. Thats why the above alternative will work for time being.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top