更改默认的Rails text_area帮助器行/列 [英] Change default Rails text_area helper rows/cols

查看:43
本文介绍了更改默认的Rails text_area帮助器行/列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现自己在所有text_area表单助手中都指定了:rows => 5,因此我查看了其定义,发现DEFAULT_TEXT_AREA_OPTIONS是指示这些选项的哈希值.但是,哈希具有冻结方法,因此我对其进行了查找,这意味着它无法更改.如果您可以向我推荐一些选项来尝试对所有文本区域进行应用范围内的:rows => 5,我将不胜感激.

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.

谢谢

推荐答案

您可以这样做:

  1. 编写自己的助手:

  1. Write own helper:

def visible_text_area(表单,方法,选项= {}) form.text_area(方法,选项) 结束

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

或使用适当的选项重新定义text_area方法委托给原始text_area

or redefine text_area method delegating to original text_area with proper options

或使用您自己的方法"my_text_area"扩展ActionView :: Helpers :: InstanceTagMethods,并使用适当的选项委托给原始text_area.然后,您可以使用"f.my_text_area(...)"

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(...)"

或更改DEFAULT_TEXT_AREA_OPTIONS:

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

选项1最干净. 2& 3个补丁已知的公共接口-似乎可以接受.内部有4个补丁-冒险.

Option 1 is most clean. 2 & 3 patch known public interface - seems acceptable. 4 patches internals - risky.

这篇关于更改默认的Rails text_area帮助器行/列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆