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.

Helper methods naming collision

#5565

Hello,

Im having a problem with helper methods with the same name on different controller views.

If I define a method 'foo' on helpers from controllers 'A' and 'B', and then call method 'foo' from a view on controller 'B' it is executing the code defined on the helper for controller 'A' views.

Reported by Adrian · September 6th, 2010 @ 10:17 PM

State: invalid
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. Rafael Mendonça França
    Rafael Mendonça França

    I could reproduce this issue in rails 3.0.0.

    module AHelper
      def foo
        'A'
      end
    end
    
    module BHelper
      def foo
        'B'
      end
    end
    
    class AController < ...
      def index
        render :inline => '<%= foo %>'
      end
    
    class BController < ...
      def index
        render :inline => '<%= foo %>'
      end
    end
    
    /a => 'B'
    /b => 'B'
    

    If I create the helper using block help in controller the rails render the currect letter, like this

    class AController < ...
      helper { def foo; 'A'; end }
      def index
        render :inline => '<%= foo %>'
      end
    
    class BController < ...
      helper { def foo; 'B'; end }
      def index
        render :inline => '<%= foo %>'
      end
    end
    
    /a => 'A'
    /b => 'B'
    

    September 7th, 2010 @ 06:06 AM

  2. Rafael Mendonça França
    Rafael Mendonça França

    In rails master this works fine.

    I attached a patch with failing test in 3-0-stable branch.

    September 7th, 2010 @ 06:41 AM

  3. David Trasbo
    David Trasbo

    Rafael,

    This does not work on master either:

    $ script/rails g controller a index
    $ script/rails g controller b index
    

    routes.rb:

    Ticket5565::Application.routes.draw do
      resources :a
      resources :b
    end
    

    Helpers:

    module AHelper
      def foo
        'A'
      end
    end
    
    module BHelper
      def foo
        'B'
      end
    end
    

    In both views:

    <%= foo %>
    

    http://localhost:3000/a => "B" http://localhost:3000/b => "B"

    September 7th, 2010 @ 12:55 PM

  4. Rohit Arondekar
    Rohit Arondekar
    • State changed from new to invalid
    • Importance changed from to Low

    You need to do clear_helpers in the conflicting controller like so:

      class AsController < ApplicationController
        clear_helpers
        .
        .
        .
      end
    

    This will remove all existing helpers except the ones defined for that class. In the above case AsHelper

    Related ticket: #5348 Visibility of helpers seems all wrong

    September 7th, 2010 @ 02:12 PM