This project is archived and is in readonly mode.

#5565 ✓invalid
Adrian

Helper methods naming collision

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

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.

Comments and changes to this ticket

  • Rafael Mendonça França

    Rafael Mendonça França September 7th, 2010 @ 06:06 AM

    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'
    
  • Rafael Mendonça França

    Rafael Mendonça França September 7th, 2010 @ 06:41 AM

    In rails master this works fine.

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

  • David Trasbo

    David Trasbo September 7th, 2010 @ 12:55 PM

    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"

  • Rohit Arondekar

    Rohit Arondekar September 7th, 2010 @ 02:12 PM

    • 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

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Attachments

Pages