如何使用Ruby on Rails获取当前控制器的模型实例?
- 内容介绍
- 文章标签
- 相关推荐
本文共计210个文字,预计阅读时间需要1分钟。
我正在构建控制器,其中需要引用当前控制器的相关模型。因此,如果我有类似以下内容:
rubyclass UsersController
def bla_s endend
简化后可以是:
rubyController for building with model references, using:class UsersController include ConcernedModule concern :Concern extend ActiveSupport::Concern def bla_s endend
我正在构建控制器问题,在其中我需要引用当前控制器的相关模型.所以,如果我有类似的东西:class UsersController < ApplicationController include Concern end module Concern extend ActiveSupport::Concern def bla self.model # ?? end end
我想在bla中获取当前模型的引用,以便当我在UserController中包含Concern时,我得到一个User引用.
这可能在Rails中吗?
controller_name.classify.constantize
本文共计210个文字,预计阅读时间需要1分钟。
我正在构建控制器,其中需要引用当前控制器的相关模型。因此,如果我有类似以下内容:
rubyclass UsersController
def bla_s endend
简化后可以是:
rubyController for building with model references, using:class UsersController include ConcernedModule concern :Concern extend ActiveSupport::Concern def bla_s endend
我正在构建控制器问题,在其中我需要引用当前控制器的相关模型.所以,如果我有类似的东西:class UsersController < ApplicationController include Concern end module Concern extend ActiveSupport::Concern def bla self.model # ?? end end
我想在bla中获取当前模型的引用,以便当我在UserController中包含Concern时,我得到一个User引用.
这可能在Rails中吗?
controller_name.classify.constantize

