Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use custom id for check_box_tag in Rails

How do you set a custom id when using a check_box_tag helper in rails?

I have a loop which creates a bunch of checkboxes based on a collection:

- subject.syllabus_references.each do |sr|
      = check_box_tag 'question[syllabus_reference]', sr.id, :id => sr.id
      = label_tag sr.id, sr.name

I would like to set a custom id so that my Label for the checkbox works correctly but I can't seem to figure out how (:id => sr.id does not work...).

The problem might also be with the way I've defined the label, so if I can get that to reference the correct check box without setting a custom id then that would be fine also...

like image 748
Ganesh Shankar Avatar asked Mar 02 '10 02:03

Ganesh Shankar


5 Answers

I used this in my application to create checkbox tags from collection and submit an array of them:

<% @cursos.each do |c| %>
  <span class='select_curso'>
    <%= check_box_tag "vaga[curso_ids][]",
      c.id, (checked = true if form.object.curso_ids.include?(c.id)) %>
    <%= label_tag "vaga[curso_ids][][#{c.id}]", c.nome %>
  </span>
<% end %>

So in params, I have an array "curso_ids"=>["1", "3", "5"] instead of a string "curso_ids"=>"5". If you want to return a single value, use vaga[curso_id], otherwise use vaga[curso_ids][] to return an array.

like image 156
nanda Avatar answered Nov 15 '22 01:11

nanda


If you give the check box an extra parameter it will work:

= check_box_tag 'question[syllabus_reference]', 1, nil, :id => sr.id
like image 34
Will Tomlins Avatar answered Nov 15 '22 00:11

Will Tomlins


@Ganesh, in your solution the resulting params hash has the following form:

params[:question][:syllabus_reference] = {1 => 1, 2 => 2, 3 => 3, ...}

These should work better for you:

check_box_tag "question[syllabus_reference][]", sr.id, checked, {:id => "question_syllabus_reference_#{sr.id}"

Notice the third parameter (checked) is required for this to work. The resulting params array will be

params[:question][:syllabus_reference] = {1, 2, 3, ...}
like image 40
Leonel Galán Avatar answered Nov 15 '22 02:11

Leonel Galán


Think I figured it out...

- subject.syllabus_references.each do |sr|
  = check_box_tag "question[syllabus_reference][#{sr.id}]", sr.id
  = label_tag "question[syllabus_reference][#{sr.id}]", sr.name

This works but if you have a better way let me know!

like image 3
Ganesh Shankar Avatar answered Nov 15 '22 01:11

Ganesh Shankar


This was very helpful and brought my days-long search to an end. Most of what I'd found until now contained syntax and extra parameters which rails either flagged or ignored altogether. All I want to do is pass an array from my view to my controller and use checkboxes to tell the controller which array elements to process. I was able to reduce the above even further, to:

<%= check_box_tag "c[]", c.id %>

where c comes from my database:

<%= @objects.each do |c| %>

This of course passes an array of object ids to my controller pertaining only to the checked entries (entries are unchecked by default since I left out the :false parameter, which by the way produces the same result as :true), which is almost all I need.

Now I just need to pass in an object type indicator - even just a literal string will do nicely - so that the controller knows what type of object it is to process without my having to augment my model with an extra column. I'll start in on that now and post my solution but please let me know if there's a quick and easy way to do this if you already know.

like image 1
Dave Sieving Avatar answered Nov 15 '22 00:11

Dave Sieving