更改默认 Rails text_area 辅助行/列
我发现自己指定了 :rows => 5 在我所有的 text_area 表单助手上,所以我查找了它的定义并发现 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.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你可以这样做:
编写自己的助手:
def read_text_area(表单、方法、选项 = {})
form.text_area(方法, 选项)
end
或使用适当的选项重新定义委托给原始text_area的text_area方法
或使用您自己的方法“my_text_area”扩展ActionView::Helpers::InstanceTagMethods和使用适当的选项委托给原始文本区域。然后您可以使用“f.my_text_area(...)”
或更改 DEFAULT_TEXT_AREA_OPTIONS:
。
选项 1 最干净。 2& 3 补丁已知的公共接口 - 似乎可以接受。 4 个补丁内部 - 有风险。
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:
.
Option 1 is most clean. 2 & 3 patch known public interface - seems acceptable. 4 patches internals - risky.
我是以下内容的粉丝:
正如 @gertas 警告的那样,这是修补内部结构,因此存在风险。这些常量在 Rails 中偶尔会发生变化。但总体而言,这并不是什么大事。或者:
所以它确实伴随着风险。但不多,这是调整这些默认值的最直接的方法。
I'm a fan of:
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.