Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to ensure a rails f.check_box (not check_box_tag) is initially checked based on active record object attribute?
    text
    copied!<p>Having a heck of a time with this; I'm convinced I'm just not getting something completely obvious as I can't imagine this being this difficult intentionally. Everything else works in my form, but upon initial display, the check boxes are not checked when they should be. Here's the relevant erb:</p> <pre><code>&lt;%= form_for :project, :url=&gt;{:controller=&gt;'projects', :action=&gt;'update_permissions', :id=&gt;@project.id} do |f| %&gt; &lt;fieldset&gt; &lt;% @project.contributors.each do |contributor| %&gt; &lt;%= f.fields_for "contributors[#{contributor.id}]" do |c| %&gt; &lt;ul id="PermissionsList" class="permissions-grid in-line clearfix full"&gt; &lt;li&gt; &lt;ul class = "clearfix permission-row"&gt; &lt;li class="first"&gt; &lt;%= c.check_box :is_active %&gt;&lt;label for="&lt;%= contributor.id %&gt;"&gt;&lt;%= contributor.user.whole_name %&gt;&lt;/label&gt; &lt;/li&gt; &lt;% @roles.each do |role| %&gt; &lt;li&gt;&lt;%= c.radio_button :role_id, role.id, :id=&gt;"#{contributor.id}-#{role.id}" %&gt;&lt;%= label "#{contributor.id}-#{role.id}", role.role_name %&gt;&lt;/li&gt; &lt;% end %&gt; &lt;/ul&gt; &lt;% end %&gt; &lt;/li&gt; &lt;/ul&gt; &lt;% end %&gt; &lt;/fieldset&gt; </code></pre> <p>I'm passing the is_checked attribute (for the current contributor) to the check_box helper. This is the proper way to do this correct? Here's what the generated markup looks like:</p> <pre><code>&lt;ul class="clearfix permission-row"&gt; &lt;li class="first"&gt; &lt;input type="hidden" value="0" name="project[contributors[9]][is_active]"&gt;&lt;input type="checkbox" value="1" name="project[contributors[9]][is_active]" id="project_contributors_9__is_active"&gt;&lt;label for="9"&gt;Bill Hatch&lt;/label&gt; &lt;/li&gt; &lt;li&gt;&lt;input type="radio" value="1" name="project[contributors[9]][role_id]" id="9-1" style="display: none;"&gt;&lt;label for="9-1_Reviewer" style="display: none;"&gt;Reviewer&lt;/label&gt;&lt;/li&gt; &lt;li&gt;&lt;input type="radio" value="2" name="project[contributors[9]][role_id]" id="9-2" style="display: none;"&gt;&lt;label for="9-2_Tech. Reviewer" style="display: none;"&gt;Tech. reviewer&lt;/label&gt;&lt;/li&gt; &lt;li&gt;&lt;input type="radio" value="3" name="project[contributors[9]][role_id]" id="9-3" style="display: none;"&gt;&lt;label for="9-3_Contributor" style="display: none;"&gt;Contributor&lt;/label&gt;&lt;/li&gt; &lt;/ul&gt; </code></pre> <p>The value of the checkbox is 1, as it should be, so I'm a little confused as to why it's not displaying as checked. Like I said, I'm sure I'm just missing something obvious; no way can this be this hard;-)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload