mardi 12 avril 2016

Rails: where does the @controller come from?

I am working on an old plugin "menu_helper" (legacy code uses it).

http://ift.tt/1oSYeAW

The main entrance of this library is as follows,

module PluginAWeek
  module MenuHelper
    def menu_bar(options = {}, html_options = {}, &block)
      puts @controller.class
      MenuBar.new(@controller, options, html_options, &block).html
    end
  end
end

ActionController::Base.class_eval do
  helper PluginAWeek::MenuHelper
end

The code works in rails 2.3.5 without problem but fails in 4.2.6.

When I puts @controller.class, in 2.3.5, it will always returns the current controller that using this library, but in 4.2.6 it will be NillClass.

So where does this @controller come from? How do I modify in 4.2.6 to make it work.

Note: to use this, I just need to call

html = menu_bar(options,:id => 'menuid')

No any controller is passed in.

Thanks.

Aucun commentaire:

Enregistrer un commentaire