覆盖 Rails date_select 表单助手 [英] Override Rails date_select form helper

查看:54
本文介绍了覆盖 Rails date_select 表单助手的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想覆盖我的应用程序中的 date_select 表单助手(运行 Rails 3.2),从 Rails 4 date_select.

覆盖默认 Rails date_select 现在有点旧了,但我参考了这个努力让它发挥作用.

所以我在 lib/ 的文件中有以下内容,包含在 environment.rb 的底部:

ActionView::Helpers::Tags::DateSelect.class_eval 做## 注意:按照建议使用 ActionView::Helpers::DateTimeSelector# 链接的问题没有错误,但似乎也没有做任何事情.#def datetime_selector(options, html_options)日期时间 = 选项[:selected] ||值(对象) ||默认日期时间(选项)@auto_index ||= nil选项 = options.dup选项[:field_name] = @method_name选项[:include_position] = true选项[:前缀] ||= @object_nameoptions[:index] = @auto_index 如果@auto_index &&!options.has_key?(:index)DateTimeSelector.new(日期时间,选项,html_options)结尾结尾

但是,当我尝试启动我的应用时,我看到:

NameError: 未初始化的常量 ActionView::Helpers::Tags

这是在控制台播放时确认的:

1.9.3-p327 :001 >动作视图=>动作视图1.9.3-p327 :002 >ActionView::Helpers=>ActionView::Helpers1.9.3-p327 :003 >ActionView::Helpers::TagsNameError: 未初始化的常量 ActionView::Helpers::Tags来自 (irb):3来自/Users/colin/.rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.11/lib/rails/commands/console.rb:47:in `start'来自/Users/colin/.rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.11/lib/rails/commands/console.rb:8:in `start'来自/Users/colin/.rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.11/lib/rails/commands.rb:41:in `'来自 script/rails:6:in `require'来自 script/rails:6:in `<main>'

那么,为什么 ActionView::Helpers::Tags 无效,当它是定义类的位置时,我该如何正确覆盖该类以覆盖 datetime_selector 方法?><小时>

回答

在 Dan 指出我试图修补错误的文件后,解决方案非常简单 - 这段代码完成了这项工作(放置在初始化程序中):

ActionView::Helpers::InstanceTag.class_eval 做def datetime_selector(options, html_options)datetime = options.fetch(:selected) { value(object) ||默认日期时间(选项)}@auto_index ||= nil选项 = options.dup选项[:field_name] = @method_name选项[:include_position] = true选项[:前缀] ||= @object_nameoptions[:index] = @auto_index 如果@auto_index &&!options.has_key?(:index)ActionView::Helpers::DateTimeSelector.new(datetime, options, html_options)结尾结尾

解决方案

ActionView::Helpers::Tags 是 Rails 4 中的新功能(您链接到 rails master 分支,即 4.0 分支),如果您正在开发 Rails 3.2.x 应用程序,则需要查看该 3.2stable 分支的源代码.这看起来像猴子补丁的地方 date_select

I would like to override the date_select form helper in my app (running Rails 3.2), to bring in the :selected parameter from the Rails 4 date_select.

Overriding default Rails date_select is a little old now, but I was referencing this to try to make it work.

So I have the following in a file in lib/, included at the bottom of environment.rb:

ActionView::Helpers::Tags::DateSelect.class_eval do
#
# Note: Using ActionView::Helpers::DateTimeSelector as suggested in 
# linked question doesn't error, but also doesn't seem to do anything.
#
  def datetime_selector(options, html_options)
    datetime = options[:selected] || value(object) || default_datetime(options)
    @auto_index ||= nil

    options = options.dup
    options[:field_name]           = @method_name
    options[:include_position]     = true
    options[:prefix]             ||= @object_name
    options[:index]                = @auto_index if @auto_index && !options.has_key?(:index)

    DateTimeSelector.new(datetime, options, html_options)
  end

end

However when trying to start my app, I see:

NameError: uninitialized constant ActionView::Helpers::Tags

This is confirmed when playing in the console:

1.9.3-p327 :001 > ActionView
 => ActionView 
1.9.3-p327 :002 > ActionView::Helpers
 => ActionView::Helpers 
1.9.3-p327 :003 > ActionView::Helpers::Tags
NameError: uninitialized constant ActionView::Helpers::Tags
  from (irb):3
  from /Users/colin/.rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.11/lib/rails/commands/console.rb:47:in `start'
  from /Users/colin/.rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.11/lib/rails/commands/console.rb:8:in `start'
  from /Users/colin/.rvm/gems/ruby-1.9.3-p327/gems/railties-3.2.11/lib/rails/commands.rb:41:in `<top (required)>'
  from script/rails:6:in `require'
  from script/rails:6:in `<main>'

So, why isn't ActionView::Helpers::Tags valid, when that is where the class is defined, and how do I override the class properly to override the datetime_selector method?


Answer

After Dan pointed out that I was trying to monkey-patch the wrong file, the solution was fairly straight forward - this code does the job (placed in an initializer):

ActionView::Helpers::InstanceTag.class_eval do
  def datetime_selector(options, html_options)
    datetime = options.fetch(:selected) { value(object) || default_datetime(options) }

    @auto_index ||= nil

    options = options.dup
    options[:field_name]           = @method_name
    options[:include_position]     = true
    options[:prefix]             ||= @object_name
    options[:index]                = @auto_index if @auto_index && !options.has_key?(:index)

    ActionView::Helpers::DateTimeSelector.new(datetime, options, html_options)
  end
end

解决方案

ActionView::Helpers::Tags is new in Rails 4 (you link to the rails master branch, which is the 4.0 branch), where you are working on a Rails 3.2.x app, you need to look at the source code for that 3.2stable branch. This looks like the place to monkey patch date_select

这篇关于覆盖 Rails date_select 表单助手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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