Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Cannot override helpers generated by routes

#5243

In rails 3 (at least b4 and rc), you can no longer override the helpers generated by routes with your own helper files.

Rails 2.3.8

map.resources :gizmos

module GizmosHelper
  def new_gizmo_path
    puts "You can see me in the server output"
    super
  end
end

Rails 3

resources :gizmos

module GizmosHelper
  def new_gizmo_path
    puts "You cannot see me in the server output"
    super
  end
end

The reason you can no longer override the route helpers is because the order in which the helpers are included into a view has changed. The relevant code is from actionpack-3.0.0.rc\lib\abstract_controller\rendering.rb:

module AbstractController
  module Rendering
    module ClassMethods
      def view_context_class
        @view_context_class ||= begin
          controller = self
          Class.new(ActionView::Base) do
            if controller.respond_to?(:_helpers)
              include controller._helpers

              if controller.respond_to?(:_routes)
                include controller._routes.url_helpers
              end

              # TODO: Fix RJS to not require this
              self.helpers = controller._helpers
            end
          end
        end
      end
    end
  end
end

To allow overriding the route generated helpers, all that needs to be changed is to put include controller._helpers after the if.

Reported by Samuel Kadolph · July 29th, 2010 @ 08:11 PM

State: resolved
Milestone: 3.0.2
Assigned to: Andrew White Andrew White
Importance: Low

Activity

  1. Samuel Kadolph
    Samuel Kadolph

    Also, shouldn't the url helpers be moved outside of the helpers check? I don't see why having helpers disabled in a controller should prevent the routes from being loaded.

    Attached is a patch to moves route helpers outside of the if for regular helpers and does it first to allow helpers to override url helpers.

    July 29th, 2010 @ 08:33 PM

  2. Luis Correa d'Almeida
    Luis Correa d'Almeida

    Any progress on this?

    Is this expected behavior or should we expect the behavior to be the same as in 2.3.8?

    September 14th, 2010 @ 12:31 AM

  3. Andrew White
    Andrew White
    • Milestone cleared.
    • State changed from new to open
    • Assigned user set to Andrew White
    • Importance changed from to Low

    September 14th, 2010 @ 06:59 AM

  4. Jeremy Kemper
  5. Repository
  6. Repository