Set maximum length in Text field in RoR

divz picture divz · Apr 23, 2013 · Viewed 32.1k times · Source

Has anyone managed to set maximum field lenghts for text fields

How can i set the maximum length of a text field. Here is the code iam using

<%= text_field_tag(:create_text), :input_html => {:maxlength => 15, :size => 40}  %>

but I cannot seem to set the max number of characters that can be typed into the field.

Answer

M. Cypher picture M. Cypher · Apr 23, 2013

Here is how you can do it:

<%= text_field_tag 'create_text', nil, :maxlength => 15, :size => 40 %>

Source: http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-text_field_tag