Reputation: 578
First of all, I want to ask forgiveness if this question is too broad.
But I'm still a little new to Rails and want to set a max length 50 chars for text fields when I edit certain titles of certain records. I know that in every one of them, I could just do something like:
<%= f.text_field :title, maxlength: 50 %>
But I'd really like to do something more like:
<%= f.text_field :title, maxlength: HEADER_MAX_LENGTH %>
where HEADER_MAX_LENGTH
is a variable that I can re-use in other forms in other views. Is there a certain place I can make such a variable to help keep my code DRY?
Thanks!
EDIT:
I initially had the f.text_field
tags syntax wrong. Fixed 'em.
Upvotes: 0
Views: 82
Reputation: 15525
Create an entry in config/application.rb
:
config.header_max_length = 50
And use this while creating forms:
<%= f.text_field :title, nil, maxlength: Rails.application.config.header_max_length %>
To make it more DRY you could create a helper method for a custom text_field_with_maxlength
or create a custom FormBuilder.
Upvotes: 2