【问题标题】:Model name of sub controller in ApplicationController methodApplicationController 方法中子控制器的模型名称
【发布时间】:2011-01-07 12:10:23
【问题描述】:

我想在我的应用程序控制器上定义一个方法,然后我可以从任何继承自它的控制器调用该方法。在此方法中,我需要能够获取与调用该方法的任何控制器关联的模型对象。

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

这可能吗?

【问题讨论】:

    标签: model controller ruby-on-rails-3


    【解决方案1】:

    试试

    self.class.to_s.chomp "Controller"
    

    【讨论】:

      猜你喜欢
      • 2020-04-16
      • 2020-01-04
      • 2011-08-17
      • 1970-01-01
      • 1970-01-01
      • 2013-03-24
      • 2012-05-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多