Rails 中的 label_tag 和 checkbox_tag 问题
我希望我的复选框可用,因此我通常会在复选框中添加标签,以便您可以选择文本,而不必“瞄准”复选框。
问题是,如果我在 Rails 中使用嵌套属性表单怎么办?我现在有这个代码:
%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
问题是它会产生这个:
<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>
而我希望它是这样的:
<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 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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我能够将复选框与标签相匹配,如下所示:
在复选框中:
我使用了 check_box_tag,将特定的项目名称(此处的角色)指定为数组的索引,以生成其中包含值的 id。我传递了 :name 哈希值来覆盖 check_box_tag 中指定的名称,这样它就没有 id: ,
它生成以下 HTML:
在标签中,我使用数组名称 + '_' 加项目指定了 id name(此处为角色),以正确指向标签中的 id:
生成以下 HTML:
当 PUT 发送到控制器时,参数具有以下字段:
这是所有已检查角色的角色名称。
因此,对于您的代码,我会尝试以下操作:
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:
which generates the following HTML:
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:
which generates the following HTML:
When the PUT is sent to the controller, the params have the following fields:
which is the role names for all of the checked roles.
Thus for your code, I would try the following:
这对我来说非常有用:
不需要关闭“do”块。由于缩进,它会自动关闭。
查看 Rails API 的最后一个示例:
http://api.rubyonrails.org/classes/ActionView /Helpers/FormHelper.html#method-i-label
This worked great for 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
执行嵌套表单时,您需要使用
fields_for
来确保字段命名正确(而不是each
)。我建议看看 formtastic 和 cocoon:这将大大简化嵌套表单的操作。
[编辑:自己做]
我不确定我可以纠正你的代码,因为你做了一些奇怪的错误:我不明白你想要检查哪个值?你不发送它。它不可能是 id。所以通常你会写这样的内容:
更正您的代码只需:
字段
is_checked?
返回某种布尔值。我认为如果您能提供更多有关您正在尝试做的事情的信息,将会有所帮助。这将生成类似于
给定的
is_checked?
将返回true
的内容。如果您想显式地以不同的方式命名
id
和name
,则需要显式设置,如下所示When doing nested forms, you need to the
fields_for
to make sure your fields are named correctly (instead of theeach
).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:
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
given
is_checked?
would returntrue
.If you want to explicitly name
id
andname
differently, you will need to set the explicitly, like so