adding a class to a text_field_tag
Asked Answered
K

3

34

I am trying to give a class to my 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=>&quot;form-control&quot;}">

What am I doing wrong ?

Khosrow answered 31/8, 2015 at 19:12 Comment(1)
please check following link.may be it will help #29198995Elkins
C
66

You declared it wrong. Here is the one will work.

<%= text_field_tag :login_aei, "", class: 'form-control' %>

I gave the value of the input field as empty string, but you can give any value what meets your business needs.

Cassel answered 31/8, 2015 at 19:13 Comment(1)
Works prefectlyDiplomatic
M
2

In the case of data binding is needed,

<%= text_field_tag(:personName,"#{@person.name}", class:'form-control', placeholder: 'User Name' )%>
Moulmein answered 30/6, 2018 at 16:29 Comment(0)
M
0

text_field_tag is an ActionView method. First thing to do is check the documentation. It says, text_field_tag takes this form:

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

Additionally:

  1. value is 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.

  2. name is a string. While Rails currently converts a symbols into strings here, consider using a string instead of a symbol for better future-proofing.

  3. 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.

Maurine answered 21/10, 2018 at 17:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.