Rails 中的 label_tag 和 checkbox_tag 问题

发布于 2024-10-18 00:41:07 字数 784 浏览 14 评论 0原文

我希望我的复选框可用,因此我通常会在复选框中添加标签,以便您可以选择文本,而不必“瞄准”复选框。

问题是,如果我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

春花秋月 2024-10-25 00:41:07

我能够将复选框与标签相匹配,如下所示:

在复选框中:
我使用了 check_box_tag,将特定的项目名称(此处的角色)指定为数组的索引,以生成其中包含值的 id。我传递了 :name 哈希值来覆盖 check_box_tag 中指定的名称,这样它就没有 id: ,

check_box_tag "user[roles][#{role.to_s}]", role.to_s, @user.has_role?(role), :name => "user[roles][]"

它生成以下 HTML:

<input id="user_roles_ROLE1" name="user[roles][]" type="checkbox" value="ROLE1" />

在标签中,我使用数组名称 + '_' 加项目指定了 id name(此处为角色),以正确指向标签中的 id:

label_tag "user_roles_#{role}", cur_role.role_name, :class => 'span_after_label'

生成以下 HTML:

<label class="span_after_label" for="user_roles_ROLE1">User</label>

当 PUT 发送到控制器时,参数具有以下字段:

"user"=>{ ..., "roles"=>["ROLE1", "ROLE2", "ROLE3"], ... }

这是所有已检查角色的角色名称。

因此,对于您的代码,我会尝试以下操作:

check_box_tag "test_case_id[#{test_case.id}]", "#{test_case.id}", test_case.id, :name => "test_case_id[]"

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:

check_box_tag "user[roles][#{role.to_s}]", role.to_s, @user.has_role?(role), :name => "user[roles][]"

which generates the following HTML:

<input id="user_roles_ROLE1" name="user[roles][]" type="checkbox" value="ROLE1" />

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:

label_tag "user_roles_#{role}", cur_role.role_name, :class => 'span_after_label'

which generates the following HTML:

<label class="span_after_label" for="user_roles_ROLE1">User</label>

When the PUT is sent to the controller, the params have the following fields:

"user"=>{ ..., "roles"=>["ROLE1", "ROLE2", "ROLE3"], ... }

which is the role names for all of the checked roles.

Thus for your code, I would try the following:

check_box_tag "test_case_id[#{test_case.id}]", "#{test_case.id}", test_case.id, :name => "test_case_id[]"
怀里藏娇 2024-10-25 00:41:07

这对我来说非常有用:

= f.label :remember_me do
    = f.check_box :remember_me
    Remember me

不需要关闭“do”块。由于缩进,它会自动关闭。
查看 Rails API 的最后一个示例:
http://api.rubyonrails.org/classes/ActionView /Helpers/FormHelper.html#method-i-label

This worked great for me:

= f.label :remember_me do
    = f.check_box :remember_me
    Remember 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

想你只要分分秒秒 2024-10-25 00:41:07

执行嵌套表单时,您需要使用 fields_for 来确保字段命名正确(而不是 each)。

我建议看看 formtasticcocoon:这将大大简化嵌套表单的操作。

[编辑:自己做]

我不确定我可以纠正你的代码,因为你做了一些奇怪的错误:我不明白你想要检查哪个值?你不发送它。它不可能是 id。所以通常你会写这样的内容:
更正您的代码只需:

check_box_tag "test_case[#{test_case.id}]", test_case.is_checked?

字段 is_checked? 返回某种布尔值。我认为如果您能提供更多有关您正在尝试做的事情的信息,将会有所帮助。

这将生成类似于

<input type="checkbox" value="true" name="test_case[70]" id="test_case_70" checked="checked">

给定的 is_checked? 将返回 true 的内容。

如果您想显式地以不同的方式命名 idname,则需要显式设置,如下所示

check_box_tag "test_case[#{test_case.id}]", test_case.is_checked?, test_case.is_checked?, :name => 'bla', :id => 'other_bla'

When doing nested forms, you need to the fields_for to make sure your fields are named correctly (instead of the each).

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:

check_box_tag "test_case[#{test_case.id}]", test_case.is_checked?

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

<input type="checkbox" value="true" name="test_case[70]" id="test_case_70" checked="checked">

given is_checked? would return true.

If you want to explicitly name id and name differently, you will need to set the explicitly, like so

check_box_tag "test_case[#{test_case.id}]", test_case.is_checked?, test_case.is_checked?, :name => 'bla', :id => 'other_bla'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文