How to add a default class to text_field in rails 3
forms, ruby-on-rails
Solution
In order to get this behavior you will either have to overwrite the existing text field method or add a new method that does what you want. I would recommend the latter since you won't be changing the existing behavior of a built-in Rails method.
Similar to another answer:
class ActionView::Helpers::FormBuilder
def inputbox_field(method, options = {})
text_field(method, options.merge(class: 'inputbox'))
end
end
Then you would just change you view to use this instead:
<%= f.inputbox_field :title %>
Problem
How to change default generated code for rails form helpers? Code... ``` <%= f.text_field :title, class: 'inputbox' %> ``` ...generates... ``` <input id="post_title" name="post[title]" class="inputbox" type="text"> ``` ...but I would like that text_field without class argument generated code above by default. I am using rails 3.2.