I am finding myself specifying :rows => 5 on all my text_area form helpers, So I looked up its definition and found the DEFAULT_TEXT_AREA_OPTIONS to be the hash dictating these options. However, the hash has this freeze method on it and I looked it up, it means it can't be changed. If you could recommend me some options to try to do a app-wide :rows => 5 for all text area, I'd really appreciate it.
Thanks
You can do:
Write own helper:
def readable_text_area(form, method, options = {}) form.text_area(method, options) end
or redefine text_area method delegating to original text_area with proper options
or extend ActionView::Helpers::InstanceTagMethods with your own method "my_text_area" and delegate to original text_area with proper options. Then you can use "f.my_text_area(...)"
or change DEFAULT_TEXT_AREA_OPTIONS:
.
module ActionView::Helpers::InstanceTagMethods
remove_const :DEFAULT_TEXT_AREA_OPTIONS
DEFAULT_TEXT_AREA_OPTIONS = { "cols" => 40, "rows" => 5 }
end
Option 1 is most clean. 2 & 3 patch known public interface - seems acceptable. 4 patches internals - risky.
I'm a fan of:
class ActionView::Helpers::InstanceTag
silence_warnings do
DEFAULT_FIELD_OPTIONS = {}
DEFAULT_TEXT_AREA_OPTIONS = {}
end
end
As @gertas warned this is patching internals so it comes with risk. These constants have moved around in Rails occasionally. But overall it is not a huge deal. Either:
So it does come with risk. But not much and it the most straight forward way to adjust these defaults.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With