How to make sure that rails f.check_box (not check_box_tag) are initially checked based on the active attribute of the recording object?

Having time with this; I am convinced that I simply won’t get something completely obvious, because I can’t imagine it to be difficult. Everything else works in my form, but on initial display, the checkboxes are not checked when they should be. Here's the corresponding erb:

<%= form_for :project, :url=>{:controller=>'projects', :action=>'update_permissions', :id=>@project.id} do |f| %> <fieldset> <% @project.contributors.each do |contributor| %> <%= f.fields_for "contributors[#{contributor.id}]" do |c| %> <ul id="PermissionsList" class="permissions-grid in-line clearfix full"> <li> <ul class = "clearfix permission-row"> <li class="first"> <%= c.check_box :is_active %><label for="<%= contributor.id %>"><%= contributor.user.whole_name %></label> </li> <% @roles.each do |role| %> <li><%= c.radio_button :role_id, role.id, :id=>"#{contributor.id}-#{role.id}" %><%= label "#{contributor.id}-#{role.id}", role.role_name %></li> <% end %> </ul> <% end %> </li> </ul> <% end %> </fieldset> 

I pass the is_checked attribute (for the current contributor) to the check_box helper. Is this the right way to do it right? Here's what the generated markup looks like:

 <ul class="clearfix permission-row"> <li class="first"> <input type="hidden" value="0" name="project[contributors[9]][is_active]"><input type="checkbox" value="1" name="project[contributors[9]][is_active]" id="project_contributors_9__is_active"><label for="9">Bill Hatch</label> </li> <li><input type="radio" value="1" name="project[contributors[9]][role_id]" id="9-1" style="display: none;"><label for="9-1_Reviewer" style="display: none;">Reviewer</label></li> <li><input type="radio" value="2" name="project[contributors[9]][role_id]" id="9-2" style="display: none;"><label for="9-2_Tech. Reviewer" style="display: none;">Tech. reviewer</label></li> <li><input type="radio" value="3" name="project[contributors[9]][role_id]" id="9-3" style="display: none;"><label for="9-3_Contributor" style="display: none;">Contributor</label></li> </ul> 

The checkbox value is 1, as it should be, so I'm a little confused as to why it does not appear as verified. As I said, I’m sure I just missed something obvious; in no way can it be so complicated; -)

+4
source share
5 answers

There are several errors in this form.

First of all, why don't you use rails to iterate over? Secondly, html identifiers cannot begin with a digit. This approach should take care of the correct filling of the values, since you use the form builder and do not insert the values ​​manually.

 <%= form_for @project, :url => update_permissions_project_path(@project) do |f| <fieldset> <%= f.fields_for :contributors do |c| %> <ul id="PermissionsList" class="permissions-grid in-line clearfix full"> <li> <ul class = "clearfix permission-row"> <li class="first"> <%= c.check_box :is_active %> <%= c.label :is_active, c.object.user.whole_name %> </li> <% @roles.each do |role| %> <li> <%= c.radio_button :role_id %> <%= c.label :role_id, role.role_name %> </li> <% end %> </ul> </li> </ul> <% end %> </fieldset> <% end %> 
+4
source

The check_box helper takes more than one argument. You need to pass the value "checked_value".

http://apidock.com/rails/ActionView/Helpers/FormHelper/check_box

Sort of

 <%= c.check_box :is_active, {}, @object.is_active %> 

Or you can update your "form_for" to refer to an object that has values ​​instead of the class name.

+2
source

Sometimes it’s easier to omit the tag helper and trick it a bit (the last parameter is “checked”):

 <%= check_box_tag "project[contributors[#{contributor.id}]][is_active]", '1', true %> 

It works?

0
source

Here's how Rails 4 determines whether f.check_box will be checked during initialization:

 def checked?(value) case value when TrueClass, FalseClass value == !!@checked _value when NilClass false when String value == @checked_value else if value.respond_to?(:include?) value.include?(@checked_value) else value.to_i == @checked_value.to_i end end end 

suppose check_box here: check_box(:post, :title) , then
Value @post.title
@checked_value is the value of the check_box checked_value argument (the default is '1' ).

0
source

I struggled with this, but found that I was exaggerating. If you use check_box, just set the default value to true or false for this field in your database. Now my checkbox is selected by default, but the actual value will be displayed if it is installed in my model instance.

 # For new instances, the onupdate field should default to checked check_box :onupdate rails g migration AddDefaultToWatchers class AddDefaultToWatchers < ActiveRecord::Migration def change change_column :watchers, :onupdate, :boolean, :default => true end end 
0
source

All Articles