有没有办法在text_field_tag上设置必需的属性?

时间:2021-10-05 20:28:41

In text_field one can do the following to set the required attribute.

在text_field中,可以执行以下操作来设置所需的属性。

< %= f.text_field :street, :required => true % >

<%= f.text_field:street,:required => true%>

< input id="recipe_name" name="recipe_name" type="text" required >

However, with text_field_tag, if I do the same the output html sets the value attribute instead, which is not correct.

但是,使用text_field_tag,如果我这样做,输出html会设置value属性,这是不正确的。

< %= text_field_tag :street, :required => true % >

<%= text_field_tag:street,:required => true%>

output:

输出:

< input id="recipe_name" name="recipe_name" type="text" value="{:required=>true}" >

true}”>

Is required not supported in text_field_tag? What is a good way to work around it?

text_field_tag中是否需要不支持?解决这个问题的好方法是什么?

2 个解决方案

#1


32  

Try: text_field_tag(name, value = nil, options = {})

尝试:text_field_tag(name,value = nil,options = {})

<%= text_field_tag :street, nil, :required => true %>

When you provide options to the helper, you have to pass the value for value parameter.

为助手提供选项时,必须传递value参数的值。

#2


0  

Text field tag

文本字段标记

Try this:

尝试这个:

<%= text_field_tag :street, '', :required => true %>

#1


32  

Try: text_field_tag(name, value = nil, options = {})

尝试:text_field_tag(name,value = nil,options = {})

<%= text_field_tag :street, nil, :required => true %>

When you provide options to the helper, you have to pass the value for value parameter.

为助手提供选项时,必须传递value参数的值。

#2


0  

Text field tag

文本字段标记

Try this:

尝试这个:

<%= text_field_tag :street, '', :required => true %>