label_tag and checkbox_tag problem in rails
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>
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[]"
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
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'
精彩评论