ruby on rails - Get checked status of checkbox in controller -
i have form in index-view create multiple checkboxes. 1 checkbox every entry. looks this:
index.html.erb
<%= form_for :user, url: usersupdate_path() |f| %> <%= render @users %> <%= f.submit 'test', :class => 'btn btn-primary' %> <% end %>
_user.html.erb
<%= check_box_tag "checked[#{user.id}]","#{user.id}",true %>
description: form want allow admin uncheck users - users want send controller , update attributes.
there 2 problems:
1) have refresh site until can send form controller - don't know why
2) when print array looks this:
{"1"=>"1", "2"=>"2", "4"=>"4"}
user 3 unchecked me.
what want this:
{"1"=>"true", "2"=>"true", "3"=>"false", "4"=>"true"}
but how can send checked
value of checkbox controller?
in controller @ moment:
def update flash[:success] = params[:checked] redirect_to root_path end
thanks
the browser not serialize unchecked checkbox when sending form data, if not checked, never gets sent.
you can fix 2 ways. make action smart enough see "missing" values "unchecked", or add hidden field before each checkbox:
<%= hidden_field_tag "checked[#{user.id}]", "false" %> <%= check_box_tag "checked[#{user.id}]","#{user.id}", true %>
as true-values, second parameter check_box_tag
value want checkbox have, can change this:
<%= hidden_field_tag "checked[#{user.id}]", "false" %> <%= check_box_tag "checked[#{user.id}]","true", true %>
and should want.
note if use formbuilder
s handle nuance you.
Comments
Post a Comment