Override Rails date_select form helper

2019-08-18 16:39发布

问题:

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

回答1:

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