Ruby-on-rails 在 Rails 中为 check_box_tag 使用自定义 ID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2360465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:20:30  来源:igfitidea点击:

Use custom id for check_box_tag in Rails

ruby-on-railscheckboxform-helpers

提问by Ganesh Shankar

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

在 Rails 中使用 check_box_tag 助手时如何设置自定义 ID?

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

我想设置一个自定义 ID,以便我的复选框标签正常工作,但我似乎无法弄清楚如何(:id => sr.id 不起作用...)。

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

问题也可能与我定义标签的方式有关,所以如果我可以在不设置自定义 id 的情况下获得它来引用正确的复选框,那么那也很好......

回答by nanda

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.

所以在 params 中,我有一个数组"curso_ids"=>["1", "3", "5"]而不是字符串"curso_ids"=>"5"。如果要返回单个值,请使用vaga[curso_id],否则使用vaga[curso_ids][]返回数组。

回答by Will Tomlins

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

如果你给复选框一个额外的参数,它将起作用:

= check_box_tag 'question[syllabus_reference]', 1, nil, :id => sr.id

回答by Leonel Galán

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

@Ganesh,在您的解决方案中,生成的 params 哈希具有以下形式:

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 数组将是

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

回答by Ganesh Shankar

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!

这有效,但如果您有更好的方法,请告诉我!

回答by d1jhoni1b

HAML sample

HAML 示例

Lets say you have a printobject somewhere with a framedattribute, you have to display a list of printsso inside the loop you will create a custom rowand columnwith unique id on each checkboxfor updating framed

假设您在print某处有一个具有framed属性的对象,您必须prints在循环内显示一个列表,您将创建一个自定义rowcolumn在每个checkbox对象上具有唯一 id 以进行更新framed

.input.row.collapse
  = check_box_tag :framed, print.framed, nil, { id: "framed_#{print.id}" }

回答by Dave Sieving

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:

c 来自我的数据库的地方:

<%= @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 :falseparameter, which by the way produces the same result as :true), which is almostall I need.

这当然会将一组对象 id 传递给我的控制器,这些对象 id 仅与选中的条目有关(默认情况下未选中条目,因为我省略了:false参数,顺便说一下,它产生的结果与:true相同),这几乎是全部我需要。

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.

现在我只需要传入一个对象类型指示符——即使只是一个文字字符串也能很好地完成——这样控制器就知道它要处理什么类型的对象,而我不必用额外的列来扩充我的模型。我现在就开始,并发布我的解决方案,但如果您已经知道,请告诉我是否有一种快速简便的方法可以做到这一点。

回答by Dave Sieving

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 checkedrather than unchecked, without passing any additional data to the controller.

为了结束循环,我放弃了通过check_box_tag传递对象类型指示器的尝试。我添加到上一篇文章中显示的语法中的每个参数(下面??)只是导致复选框状态默认为选中而不是未选中,而没有将任何额外的数据传递给控制器​​。

While remaining alert to alternative solutions as they present themselves, for the time being I incorporate the object type into the submit_tagvalue. This ties the functionality to the display, since the submit_tagvalue 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.

虽然在替代解决方案出现时保持警惕,但暂时我将对象类型合并到submit_tag值中。这将功能与显示联系起来,因为submit_tag值是在提交按钮上向用户显示的值,但可以说这迫使视图清晰,即使它为控制器提供了所需的消歧。

Learning day by day...

天天学习...