本文介绍了Rails:如何向radio_button_tag添加空的"required"属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 radio_button_tag 生成单选按钮标签,例如:

I'm trying to use a radio_button_tag to generate a radio-button tag like:

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

为了添加表单验证(我正在使用Foundation并尝试使用Abide库).我能得到的最接近的是:

in order to add form validation (I'm using Foundation and trying to make use of the Abide library). The closest I can get is:

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

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

这对于Abide来说似乎已经足够好了,但是有什么方法可以使我从Rails助手中得到想要的东西吗?我尝试使用 required:true 代替 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? I've tried required: true in place of required: '' but it behaves the same.

推荐答案

如您在此处看到的: https://github.com/rails/rails/blob/4de8851289077239ecc91473bdba30f8cf6727bb/actionview/lib/action_view/helpers/tag_helper.rb#L149

Rails仅查找带有"if value"的值的存在.空字符串被认为是一个值,因为它不是nil,这就是触发rails包含"required ='required'"属性的原因.

Rails just looks for the presence of a value with "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.

Abide真的需要标记为空白吗?看起来它可能只是在检查必需"属性的存在,因此它可能不在乎该属性是否具有值.

Does Abide really need the tag to be blank? 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.

这篇关于Rails:如何向radio_button_tag添加空的"required"属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 04:39