Ruby-on-rails 将类添加到 text_field_tag
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32318322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
adding a class to a text_field_tag
提问by David Geismar
I am trying to give a class to my text_field_tag
我想给我的 text_field_tag 上课
I have this
我有这个
<%= text_field_tag :login_aei, class: 'form-control' %>
but it keeps generating this :
但它不断产生这个:
<input type="text" name="login_aei" id="login_aei" value="{:class=>"form-control"}">
What am I doing wrong ?
我究竟做错了什么 ?
回答by Arup Rakshit
You declared it wrong. Here is the one will work.
你声明错了。这是一个会起作用的。
<%= text_field_tag :login_aei, "", class: 'form-control' %>
I gave the value of the inputfield as empty string, but you can give any value what meets your business needs.
我将该input字段的值指定为空字符串,但您可以指定满足您业务需求的任何值。
回答by zawhtut
In the case of data binding is needed,
在需要数据绑定的情况下,
<%= text_field_tag(:personName,"#{@person.name}", class:'form-control', placeholder: 'User Name' )%>
回答by IAmNaN
text_field_tagis an ActionView method. First thing to do is check the documentation. It says, text_field_tagtakes this form:
text_field_tag是一个 ActionView 方法。首先要做的是检查文档。它说,text_field_tag采用这种形式:
text_field_tag(name, value = nil, options = {})
Additionally:
此外:
valueis the initial value for the text box. Notice the default is nil, not an empty string. By passing in a string, you are using it in an undocumented way, which may have worked in this instance but could behave subtly different.nameis a string. While Rails currentlyconverts a symbols into strings here, consider using a string instead of a symbol for better future-proofing.The two issues above were overlooked by skipping straight to SO for the quick answer. Consider RTFM as it is the only definitive source, besides the source code itself.
value是文本框的初始值。请注意,默认值是 nil,而不是空字符串。通过传入一个字符串,您以一种未记录的方式使用它,这可能在这种情况下有效,但行为可能略有不同。name是一个字符串。虽然 Rails目前在这里将符号转换为字符串,但考虑使用字符串而不是符号以更好地面向未来。通过直接跳到 SO 以获得快速答案,上述两个问题被忽略了。考虑 RTFM,因为它是除源代码本身之外唯一的权威来源。

