简体   繁体   English

Rails:如何将空的“必需”属性添加到 radio_button_tag

[英]Rails: How to add an empty 'required' attribute to a radio_button_tag

I'm trying to use a radio_button_tag to generate a radio-button tag like:我正在尝试使用radio_button_tag生成一个单选按钮标签,如:

<input type="radio" ... required>

in order to add form validation (I'm using Foundation and trying to make use of the Abide library).为了添加表单验证(我正在使用 Foundation 并尝试使用 Abide 库)。 The closest I can get is:我能得到的最接近的是:

radio_button_tag 'my_radio', ... , required: ''

# => <input type="radio" ... required="required">

This seems to be good enough for Abide to work, but is there a way I can get what I want out of the Rails helper?这似乎足以让 Abide 工作,但是有什么方法可以让我从 Rails 助手中获得我想要的东西? I've tried required: true in place of required: '' but it behaves the same.我试过required: true代替required: ''但它的行为是一样的。

复制/粘贴没有字面上的答案,所以我提供了一个。

<%= radio_button_tag(:model_id, model.id, false, required: 'required') %>

As you can see here: https://github.com/rails/rails/blob/4de8851289077239ecc91473bdba30f8cf6727bb/actionview/lib/action_view/helpers/tag_helper.rb#L149正如你在这里看到的: https : //github.com/rails/rails/blob/4de8851289077239ecc91473bdba30f8cf6727bb/actionview/lib/action_view/helpers/tag_helper.rb#L149

Rails just looks for the presence of a value with "if value". Rails 只是寻找带有“if value”的值的存在。 A blank string is considered a value since it isn't nil and that is what triggers rails to include that "required='required'" attribute.一个空字符串被认为是一个值,因为它不是 nil,这就是触发 rails 包含“required='required'”属性的原因。

Does Abide really need the tag to be blank? Abide 真的需要标签为空吗? It looks like it may just be checking for the presence of the "required" attribute so it may not care if the attribute has a value or not.看起来它可能只是在检查“必需”属性的存在,因此它可能不关心该属性是否具有值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM