text_field
![Very extensive documentation Importance_5](https://d2vfyqvduarcvs.cloudfront.net/images/importance_5.png?1349367920)
text_field(object_name, method, options = {})
public
Returns an input tag of the “text” type tailored for accessing a specified attribute (identified by method) on an object assigned to the template (identified by object). Additional options on the input tag can be passed as a hash with options. These options will be tagged onto the HTML as an HTML element attribute as in the example shown.
Examples
text_field(:post, :title, size: 20) # => <input type="text" id="post_title" name="post[title]" size="20" value="#{@post.title}" /> text_field(:post, :title, class: "create_input") # => <input type="text" id="post_title" name="post[title]" value="#{@post.title}" class="create_input" /> text_field(:post, :title, maxlength: 30, class: "title_input") # => <input type="text" id="post_title" name="post[title]" maxlength="30" size="30" value="#{@post.title}" class="title_input" /> text_field(:session, :user, onchange: "if ($('#session_user').val() === 'admin') { alert('Your login cannot be admin!'); }") # => <input type="text" id="session_user" name="session[user]" value="#{@session.user}" onchange="if ($('#session_user').val() === 'admin') { alert('Your login cannot be admin!'); }"/> text_field(:snippet, :code, size: 20, class: 'code_input') # => <input type="text" id="snippet_code" name="snippet[code]" size="20" value="#{@snippet.code}" class="code_input" />
![Default_avatar_30](https://www.gravatar.com/avatar/855c677aca7319a44da19fb583b9f320?default=http://apidock.com/images/default_avatar_30.png&size=30)
Force initial value
If you want to force an initial value for your text_field which is normally based on your object attribute value, you can use :
text_field :ecard, :sender, :value => 'contact@host.com'
![Default_avatar_30](https://www.gravatar.com/avatar/684a52b6b20c7eb009c61bb7ef7cc475?default=http://apidock.com/images/default_avatar_30.png&size=30)
Found out you can't set the type to number
I am sure others have tried to do this but I thought I would add a not that tells ever one. You can’t use the type field in in rails 2.3. It is always set back to type => “text”
So if you are trying to set it to type => “number” it will not work. It always goes back to text. Sorry only way around this is to use a text_field_tag and set the type in that.
<%= text_field_tag :thing, type: "number" %>
Code above is the only way to set the type. in rails 3 you can just call number_field and it will work
![Default_avatar_30](https://www.gravatar.com/avatar/5866464ebc16b5c4729fff4fe7d8fbe7?default=http://apidock.com/images/default_avatar_30.png&size=30)
Alternative force initial value
@Bounga - Thanks for the suggestion. Didn’t know about that method. An alternative method I have used is just to assign the default value to the object in your controller. For example your “new” action might now look like:
def new @ecard = Ecard.new params[:ecard] @ecard.sender ||= 'contact@host.com' end
![Default_avatar_30](https://www.gravatar.com/avatar/6ee18d3226b5a7870df7c56cdadfc24b?default=http://apidock.com/images/default_avatar_30.png&size=30)
Re: Force initial value
An alternative to @eric_programmer’s would be to extract it entirely from the controller logic…
class Person def sender self[:sender] || 'contact@host.com' end end
There are a ton of ways to do it, but making this a business logic decision will let you get the same logic from any possible implementation angle. Scripts, web service, etc.
![Default_avatar_30](https://www.gravatar.com/avatar/7c741435055373da2052d57d650403cd?default=http://apidock.com/images/default_avatar_30.png&size=30)
BEWARE of options[:value] in a partial
No problem with new records however,
the following line will not display the current @ecard value on EDIT but ‘contact@host.com’.
text_field :ecard, :sender, :value => 'contact@host.com'
Don’t let your views take control, but if you insist:
text_field :ecard, :sender, :value => f.object.ecard.blank? ? 'contact@host.com' : f.object.ecard
This doesn’t make sense, since when this object was created they deleted the default, which made it blank. Why would they want it back?