Changing the default Rails text_area auxiliary rows / columns

I found myself pointing out: rows => 5 on all my helpers of the text_area form. Therefore, I looked at its definition and found a DEFAULT_TEXT_AREA_OPTIONS hash dictating these parameters. However, the hash has this freeze method on it, and I was looking for it, which means that it cannot be changed. If you could recommend me some options to try to make the application as a whole: rows => 5 for the entire text area, I would really appreciate it.

thanks

+4
source share
2 answers

You can do:

  • Create your own assistant:

    def readable_text_area (form, method, options = {}) form.text_area (method, parameters) end

  • or override the delegation text_area method to the original text_area with the appropriate parameters

  • or extend ActionView :: Helpers :: InstanceTagMethods with your "my_text_area" method and pass the original text_area with the correct parameters. 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 the cleanest. 2 and 3, the well-known public interface - seems acceptable. 4 internal patches are risky.

+6
source

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 a fix for internal components, so it carries a risk. Sometimes these constants moved to Rails. But overall, this is not a huge deal. Or:

  • You use CSS for width and height so that all of these attributes are ignored. In this case, all you do is save a few useless characters moving around the screen. If it stops working, you just spend a few extra characters until you notice it.
  • You use these attributes, so when the hack stops working, it becomes obvious (the size of the field changes), and you can quickly fix it.

Thus, it is associated with risk. But not so much, and this is the most direct way to set these defaults.

+2
source

All Articles