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