Ruby on Rails:如何从控制器内部获取模型名?

时间:2023-01-14 14:28:32

Let's say the controller name is TemplateUserController whose model class is TemplateUser.

假设控制器名是TemplateUserController,它的模型类是TemplateUser。

Now, I could do self.name.tableize.singularize.string_manipulation… but that seems a little excessive… I was wondering if there was a faster way to get the model name from the controller. Thanks! =D

现在,我可以执行self。name. tableizy .singularize.string_manipulation……但是这似乎有点过分了……我想知道是否有一种更快的方法从控制器获取模型名。谢谢!= D

4 个解决方案

#1


5  

A more direct way to do this: controller_name.classify.

更直接的方法是:controller_name. classification。

#2


2  

You probably know that you can't guarantee a 1-to-1 mapping between controllers and models.

您可能知道,您不能保证控制器和模型之间的映射是1比1。

However, in the cases where you can, CanCan is a gem which needs to do the same thing you're after, and it does it like this:

然而,在你可以的情况下,CanCan是一颗宝石,需要做你想做的事情,它是这样做的:

def model_name
  params[:controller].sub("Controller", "").underscore.split('/').last.singularize
end

Because there isn't an implied link between model and controller (except by convention), making your own judgements based on the controller name is the only way to go.

因为在模型和控制器之间没有隐含的联系(除了约定),基于控制器名做出自己的判断是唯一的方法。

#3


0  

params[:controller]
#=> "TemplateUserController"
params[:controller][/.*[^Controller]/]
#=> "TemplateUser"

params[:controller]
#=> "UsersController"
params[:controller][/.*[^Controller]/].singularize
#=> "User"

And yes - in real world there are many of controllers don't refer to model

是的,在现实世界中有很多控制器都不参照模型

PS

PS

Istead of params[:controller] you can use Rails.controller

可以使用rails.com .controller来代替params[:controller]

#4


0  

@Sam's solution of controller_name.classify.constantize works for all controllers, although many like me may have to lookup the doc to realize that 'controller' is not part of the name.

@Sam controller_name.classify的解决方案。constantize适用于所有控制器,但像我这样的许多人可能需要查找doc,才能意识到“controller”不是名称的一部分。

If your controller is to control a model though, you should consider using inherited_resources. You get many benefits, one of which is the method resource_class:

如果您的控制器是控制一个模型,那么您应该考虑使用继承资源。你会得到很多好处,其中之一是方法resource_class:

class TemplateUserController < InheritedResources::Base
  resource_class # => TemplateUser
end

#1


5  

A more direct way to do this: controller_name.classify.

更直接的方法是:controller_name. classification。

#2


2  

You probably know that you can't guarantee a 1-to-1 mapping between controllers and models.

您可能知道,您不能保证控制器和模型之间的映射是1比1。

However, in the cases where you can, CanCan is a gem which needs to do the same thing you're after, and it does it like this:

然而,在你可以的情况下,CanCan是一颗宝石,需要做你想做的事情,它是这样做的:

def model_name
  params[:controller].sub("Controller", "").underscore.split('/').last.singularize
end

Because there isn't an implied link between model and controller (except by convention), making your own judgements based on the controller name is the only way to go.

因为在模型和控制器之间没有隐含的联系(除了约定),基于控制器名做出自己的判断是唯一的方法。

#3


0  

params[:controller]
#=> "TemplateUserController"
params[:controller][/.*[^Controller]/]
#=> "TemplateUser"

params[:controller]
#=> "UsersController"
params[:controller][/.*[^Controller]/].singularize
#=> "User"

And yes - in real world there are many of controllers don't refer to model

是的,在现实世界中有很多控制器都不参照模型

PS

PS

Istead of params[:controller] you can use Rails.controller

可以使用rails.com .controller来代替params[:controller]

#4


0  

@Sam's solution of controller_name.classify.constantize works for all controllers, although many like me may have to lookup the doc to realize that 'controller' is not part of the name.

@Sam controller_name.classify的解决方案。constantize适用于所有控制器,但像我这样的许多人可能需要查找doc,才能意识到“controller”不是名称的一部分。

If your controller is to control a model though, you should consider using inherited_resources. You get many benefits, one of which is the method resource_class:

如果您的控制器是控制一个模型,那么您应该考虑使用继承资源。你会得到很多好处,其中之一是方法resource_class:

class TemplateUserController < InheritedResources::Base
  resource_class # => TemplateUser
end