更改默认 Rails text_area 辅助行/列

发布于 2024-09-24 05:18:50 字数 200 浏览 2 评论 0原文

我发现自己指定了 :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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

余生再见 2024-10-01 05:18:50

你可以这样做:

  1. 编写自己的助手:

    def read_text_area(表单、方法、选项 = {})
    form.text_area(方法, 选项)
    end

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

  3. 或使用您自己的方法“my_text_area”扩展ActionView::Helpers::InstanceTagMethods和使用适当的选项委托给原始文本区域。然后您可以使用“f.my_text_area(...)”

  4. 或更改 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 个补丁内部 - 有风险。

You can do:

  1. Write own helper:

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

  2. or redefine text_area method delegating to original text_area with proper options

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

  4. 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.

望喜 2024-10-01 05:18:50

我是以下内容的粉丝:

class ActionView::Helpers::InstanceTag
  silence_warnings do
    DEFAULT_FIELD_OPTIONS = {}
    DEFAULT_TEXT_AREA_OPTIONS = {}
  end
end

正如 @gertas 警告的那样,这是修补内部结构,因此存在风险。这些常量在 Rails 中偶尔会发生变化。但总体而言,这并不是什么大事。或者:

  1. 您使用 CSS 来表示宽度和高度,因此这些属性无论如何都会被忽略。在这种情况下,您所做的就是避免一些无用的角色在屏幕上移动。如果它停止工作,您只需花费一些额外的字符,直到您注意到为止。
  2. 您正在使用这些属性,因此当黑客停止工作时,它会变得很明显(字段大小发生变化),您可以快速修复它。

所以它确实伴随着风险。但不多,这是调整这些默认值的最直接的方法。

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:

  1. You are using CSS for width and height so these attributes are being ignored anyway. In that case all you are doing is saving a few useless characters from moving across the screen. If it stops working you just spend a few extra characters until you notice it.
  2. You are using these attributes so when the hack stops working it becomes obvious (the field size changes) and you can fix it quickly.

So it does come with risk. But not much and it the most straight forward way to adjust these defaults.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文