mysql - Simple form multiple select with checkboxes -
i've been trying build search form allows users search selection multiple checkbox selections in drop down list. problem when submit form, values getting passed in array 1 empty value @ end , when check logs query looks this
update `searches` set `ethnicity` = '---\n- pacific islander\n- white\n- other\n- \'\'\n', `updated_at` = '2014-04-21 18:24:03' `searches`.`id` = 1
i dont understand why have characters around each form value. using simple form , form this
<%= simple_form_for(:search, :url => {:controller => 'searches', :action => 'update', :slug => session[:username]}) |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 array understanding , 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 posting section of code not working form has other fields such gender, height, age, etc work fine. i'm having problems when use checkboxes. radio buttons , select fields work perfect.
here 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 using domain: localhost user load (0.5ms) select `users`.* `users` `users`.`id` = 1 limit 1 search load (0.3ms) select `searches`.* `searches` `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' `searches`.`id` = 1
im totally beginner building way out of league i've researched problem thoroughly , need more help. can explain why isnt working , need do? thanks!
i tried removing :include_blank => "anything" received same log output
processing searchescontroller#update 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 using domain: localhost user load (0.6ms) select `users`.* `users` `users`.`id` = 1 limit 1 search load (0.4ms) select `searches`.* `searches` `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' `searches`.`id` = 1
update checkboxes
code 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 following searchescontroller#update
action before updating
record.
## remove "" value params[:search][:ethnicity] params[:search][:ethnicity] = params[:search][:ethnicity].reject(&:empty?)
i did 2 hrs of research find out why include_hidden: false
not work expected , found out there issues raised before rails 4 release. after rails 4 release issue still continues. thats why above alternative work time being.
Comments
Post a Comment