Ruby-on-rails Rails:如何根据控制器类名获取模型类名?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4869917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:16:40  来源:igfitidea点击:

Rails: How to get the model class name based on the controller class name?

ruby-on-railsruby-on-rails-3

提问by Misha Moroshko

class HouseBuyersController < ...
  def my_method
    # How could I get here the relevant model name, i.e. "HouseBuyer" ?
  end
end

回答by malclocke

This will do it:

这将做到:

class HouseBuyersController < ApplicationController

  def index
    @model_name = controller_name.classify
  end

end

This is often needed when abstracting controller actions:

这在抽象控制器操作时经常需要:

class HouseBuyersController < ApplicationController

  def index
    # Equivalent of @house_buyers = HouseBuyer.find(:all)
    objects = controller_name.classify.constantize.find(:all)
    instance_variable_set("@#{controller_name}", objects)
  end

end

回答by Matt

If your controller and model are in the same namespace, then what you want is

如果您的控制器和模型在同一个命名空间中,那么您想要的是

controller_path.classify

controller_pathgives you the namespace; controller_namedoesn't.

controller_path给你命名空间;controller_name没有。

For example, if your controller is

例如,如果您的控制器是

Admin::RolesController

then:

然后:

controller_path.classify # "Admin::Role" # CORRECT
controller_name.classify # "Role"        # INCORRECT

回答by Scott

It's a bit of a hack, but if your model is named after your controller name then:

这有点黑客,但如果您的模型以您的控制器名称命名,那么:

class HouseBuyersController < ApplicationController
  def my_method
    @model_name = self.class.name.sub("Controller", "").singularize
  end
end

... would give you "HouseBuyer" in your @model_name instance variable.

...会在您的@model_name 实例变量中为您提供“HouseBuyer”。

Again, this makes a huge assumption that "HouseBuyersController" only deals with "HouseBuyer" models.

同样,这做出了一个巨大的假设,即“HouseBuyersController”只处理“HouseBuyer”模型。

回答by hachpai

For namespaces working:

对于命名空间工作:

def resource_class
 controller_path.classify.constantize
end

回答by thenengah

This is not possible if you are using the default MVC, which your code doesn't seem to follow. Your controller seems to be a model but maybe you just got a type there. Anyway, controllers and models are fundamentally separated in Rails MVC so controllers cannot know which model they are associated with.

如果您使用的是默认 MVC,这是不可能的,您的代码似乎没有遵循。您的控制器似乎是一个模型,但也许您只是在那里得到了一个类型。无论如何,控制器和模型在 Rails MVC 中从根本上是分开的,因此控制器无法知道它们与哪个模型相关联。

For example you could have a model named post. This can have a controller posts_controller or could have a controller like articles_controller. Rails only knows about models when you def the actual code in the controller such as

例如,您可以有一个名为 post 的模型。这可以有一个控制器posts_controller 或可以有一个控制器,如articles_controller。Rails 仅在您定义控制器中的实际代码时才知道模型,例如

def index
  @posts = Post.all
  @posts = Article.all
end  

In rails standard controllers there is no way to know what the model is.

在 rails 标准控制器中,无法知道模型是什么。

回答by stoffus

The accepted solution did not work for me as my controller and model was namespaced. Instead, I came up with the following method:

接受的解决方案对我不起作用,因为我的控制器和模型是命名空间的。相反,我想出了以下方法:

def controllers_model
  (self.class.name.split('::')[0..-2] << controller_name.classify).join('::')
end