[英]Override Rails date_select form helper
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 . 我想在我的应用程序(运行Rails 3.2)中覆盖date_select表单助手,以从Rails 4 date_select中引入
:selected
参数。
Overriding default Rails date_select is a little old now, but I was referencing this to try to make it work. 现在, 覆盖默认的Rails date_select有点老了,但是我引用这个是想使其工作。
So I have the following in a file in lib/
, included at the bottom of environment.rb
: 所以我在
lib/
的文件中包含以下内容,该文件包含在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? 那么,为什么在定义类的时候
ActionView::Helpers::Tags
无效,以及如何正确重写该类以重写datetime_selector方法?
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): 在Dan指出我试图用猴子修补错误的文件后,解决方案就非常简单了-这段代码完成了这项工作(放在初始化程序中):
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. ActionView::Helpers::Tags
是Rails 4中的新增功能(您链接到rails master分支,后者是4.0分支),在此您使用Rails 3.2.x应用程序,您需要查看该源代码3.2稳定分支 This looks like the place to monkey patch date_select 这看起来像是猴子修补date_select的地方
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.