我想在我的应用程序控制器上定义一个方法,然后我可以从任何继承它的控制器调用它。在这个方法中,我需要能够获得与调用该方法的控制器相关联的模型对象。
class ApplicationController < ActionController::Base
def bar
#Access model name here. So inside FooController, it would have Foo.
end
end
class FooController < ApplicationController
def index
bar #Use the method here
end
end
这可能吗?
答案 0 :(得分:1)
尝试
self.class.to_s.chomp "Controller"