Use custom id for check_box_tag in Rails

2019-03-18 07:36发布

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...

7条回答
Deceive 欺骗
2楼-- · 2019-03-18 08:01

Leito,

Just to close the loop, I gave up trying to pass an object type indicator in through check_box_tag. Every parameter I added to the syntax shown in my last post above (below??) simply caused the checkbox state to default to checked rather than unchecked, without passing any additional data to the controller.

While remaining alert to alternative solutions as they present themselves, for the time being I incorporate the object type into the submit_tag value. This ties the functionality to the display, since the submit_tag value is what's shown to the user on the submit button, but it could be argued that this forces clarity into the view even as it provides needed disambiguation to the controller.

Learning day by day...

查看更多
Rolldiameter
3楼-- · 2019-03-18 08:07

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!

查看更多
Summer. ? 凉城
4楼-- · 2019-03-18 08:08

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.

查看更多
相关推荐>>
5楼-- · 2019-03-18 08:08

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

= check_box_tag 'question[syllabus_reference]', 1, nil, :id => sr.id
查看更多
来,给爷笑一个
6楼-- · 2019-03-18 08:11

@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, ...}
查看更多
萌系小妹纸
7楼-- · 2019-03-18 08:18

HAML sample

Lets say you have a print object somewhere with a framed attribute, you have to display a list of prints so inside the loop you will create a custom row and column with unique id on each checkbox for updating framed

.input.row.collapse
  = check_box_tag :framed, print.framed, nil, { id: "framed_#{print.id}" }
查看更多
登录 后发表回答