9

I want my checkboxes to be usable so I usually add label fors to the checkboxes so you can select the text instead of having to "aim" for the checkbox.

Problem is, what if I am using a nested attributes form in rails? I have this code for now:

%ul
  - test_category.test_cases.each do |test_case|
    %li
      = check_box_tag "job_test_cases[]", "#{test_case.id}", test_case.id
      = label_tag "test_case_#{test_case.id}", test_case.name

problem with this is that it produces this:

<li>
  <input type="checkbox" value="70" name="job_test_cases[]" id="job_test_cases_" checked="checked">
  <label for="test_case_70">Blah blah</label>
</li>

whereas I wanted it to be like this:

<li>
  <input type="checkbox" value="70" name="test_case_id[]" id="test_case_70" checked="checked">
  <label for="test_case_70">Blah BLah blah/label>
</li>
5
  • Your output isn't at all what I expected from how you are using check_box_tag :/ Doesn't seem to match up with the docs at all api.rubyonrails.org/classes/ActionView/Helpers/… Commented Feb 16, 2011 at 6:24
  • i am confused myself -_- Commented Feb 16, 2011 at 6:36
  • oh wait i think i pasted the wrong thing..lemme edit it Commented Feb 16, 2011 at 6:41
  • 1st arg is id and name, 2nd is value, 3rd is weather it is checked (true/false), 4th are options. Not sure why you have test_case.id as the 3rd arg and it is a bit confusing to me why your id/name values don't match up. Commented Feb 16, 2011 at 7:03
  • is that possible? thats what i want to happen, because the label for is dependent on the id. but i want the name to be an array so the backend gets a collection of test_cases. Commented Feb 16, 2011 at 8:08

3 Answers 3

8

I was able to match the checkbox with the label as follows:

In the checkbox: I used the check_box_tag, specifying the specific item name (role here) as the index of the array to produce the id with a value in it. I passed the :name hash value to override the name specified in the check_box_tag, so that it does not have the id:

check_box_tag "user[roles][#{role.to_s}]", role.to_s, @user.has_role?(role), :name => "user[roles][]"

which generates the following HTML:

<input id="user_roles_ROLE1" name="user[roles][]" type="checkbox" value="ROLE1" />

In the label, I specified the id using the array name + '_' plus the item name (role here), to properly point to the id in the label:

label_tag "user_roles_#{role}", cur_role.role_name, :class => 'span_after_label'

which generates the following HTML:

<label class="span_after_label" for="user_roles_ROLE1">User</label>

When the PUT is sent to the controller, the params have the following fields:

"user"=>{ ..., "roles"=>["ROLE1", "ROLE2", "ROLE3"], ... }

which is the role names for all of the checked roles.

Thus for your code, I would try the following:

check_box_tag "test_case_id[#{test_case.id}]", "#{test_case.id}", test_case.id, :name => "test_case_id[]"
Sign up to request clarification or add additional context in comments.

Comments

3

This worked great for me:

= f.label :remember_me do
    = f.check_box :remember_me
    Remember me

There's no need to close "do" block. It gets closed automatically because of indentation. Check out the last example from Rails API: http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-label

Comments

0

When doing nested forms, you need to the fields_for to make sure your fields are named correctly (instead of the each).

I would suggest having a look at formtastic and cocoon: that will greatly simplify doing nested forms.

[EDIT: doing it yourself]

I am not sure i can correct your code, because you do something strangely wrong: i do not understand which value you want to be possibly checked?? You don't send that it in. It could not be the id. So normally you write something like: Correcting your code would just be:

check_box_tag "test_case[#{test_case.id}]", test_case.is_checked?

where the field is_checked? returns some kind of boolean value. I think it would help if you would give a little more info about what you are trying to do.

That would generate something like

<input type="checkbox" value="true" name="test_case[70]" id="test_case_70" checked="checked">

given is_checked? would return true.

If you want to explicitly name id and name differently, you will need to set the explicitly, like so

check_box_tag "test_case[#{test_case.id}]", test_case.is_checked?, test_case.is_checked?, :name => 'bla', :id => 'other_bla'

3 Comments

although i AM concerned with form field names, i do need the id not to have that array thing so i can do the labels myself. do you know how they implement it if ever?( i do know about formtastic)
I did an attempt at answering, but need more information. Not sure what you mean: "doing the labels yourself". With formtastic you can easily overrule values of labels. If it is more control over the generated html, check out simple_form. But in some cases you need to be able to do everything by hand as well.
well actually the code was done by my colleague and he needs to get an array of test_cases from the params, thus the id is test_case[] and the value is 71, 72, etc. if you submit the form you get test_cases[] => {71, 72}. i just want it to work that way with the labels, but the checkbox id ruins it because all the checkboxes must have an id of test_case[]. does that explain my plan well?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.