在Rails中为check_box_tag使用自定义ID [英] Use custom id for check_box_tag in Rails

查看:149
本文介绍了在Rails中为check_box_tag使用自定义ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在rails中使用check_box_tag帮助程序时如何设置自定义ID?

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

一个自定义id,使我的标签为该复选框工作正常,但我似乎无法弄清楚如何(:id => sr.id不工作...)。

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,那么也很好...

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

推荐答案

我在应用程序中使用这个来从集合创建checkbox标签并提交一个数组:

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

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

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.

这篇关于在Rails中为check_box_tag使用自定义ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆