title: Override Rails date_select form helper
body: <p>I would like to override the date_select form helper in my app (running Rails 3.2), to bring in the <code>:selected</code> parameter from the <a href="https://github.com/rails/rails/blob/1dd89250d12aba01d605af270201371e5cec89a4/actionpack/lib/action_view/helpers/tags/date_select.rb#L30" rel="nofollow noreferrer">Rails 4 date_select</a>.</p>
  
  <p><a href="https://stackoverflow.com/questions/4007675/overriding-default-rails-date-select">Overriding default Rails date_select</a> is a little old now, but I was referencing this to try to make it work.</p>
  
  <p>So I have the following in a file in <code>lib/</code>, included at the bottom of <code>environment.rb</code>:</p>
  
  <pre><code>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 &amp;&amp; !options.has_key?(:index)
  
      DateTimeSelector.new(datetime, options, html_options)
    end
  
  end
  </code></pre>
  
  <p>However when trying to start my app, I see:</p>
  
  <pre><code>NameError: uninitialized constant ActionView::Helpers::Tags
  </code></pre>
  
  <p>This is confirmed when playing in the console:</p>
  
  <pre><code>1.9.3-p327 :001 &gt; ActionView
   =&gt; ActionView 
  1.9.3-p327 :002 &gt; ActionView::Helpers
   =&gt; ActionView::Helpers 
  1.9.3-p327 :003 &gt; 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 `&lt;top (required)&gt;'
    from script/rails:6:in `require'
    from script/rails:6:in `&lt;main&gt;'
  </code></pre>
  
  <p>So, why isn't <code>ActionView::Helpers::Tags</code> valid, when that is where the class is defined, and how do I override the class properly to override the datetime_selector method?</p>
  
  <hr>
  
  <p><strong>Answer</strong></p>
  
  <p>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):</p>
  
  <pre><code>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 &amp;&amp; !options.has_key?(:index)
  
      ActionView::Helpers::DateTimeSelector.new(datetime, options, html_options)
    end
  end
  </code></pre>