我试图访问helper中控制器中定义的helper_method。这是在轨道2.3我的假设。在轨道3/4它不。这是对的吗?
application_controller.rb:
class ApplicationController < ActionController::Base
helper_method :some_method
end
application_helper.rb:
module ApplicationHelper
def use_some_method
some_method # trying to use helper_method defined in controller which worked in rails 2.3?
end
end
我收到此错误:
Error: undefined local variable or method `some_method' for...
如果不再允许访问helper_method的正确方法是什么?
答案 0 :(得分:0)
将 application_helper.rb 中的方法定义为: -
module ApplicationHelper
def use_some_method
some_method
end
def some_method
end
end
从 application_controller.rb
删除 some_method 方法也可以从 application_controller.rb 中删除此行: -
helper_method :some_method
答案 1 :(得分:0)
According to the docs,你可以这样做:
#app/controllers/your_controller.rb
Class YourController < ApplicationController
helper_method :some_method
def some_method
"hello world"
end
end
然后可以在您的帮助程序模块/类中访问它。我很感激你可能已经尝试过,如果它不能正常工作,可能是因为你的控制器没有加载
我要做的最基本的测试是将它放入application_controller
(这总是被调用),然后看看你是否可以从帮助文件访问该方法