开发者

What's the Rails Way to get the Model Class from the Controller?

开发者 https://www.devze.com 2023-01-21 03:28 出处:网络
I can do this in the controller to get the model class: self.class.name.gsub(\"Controller\", \"\").singularize.constantize

I can do this in the controller to get the model class:

self.class.name.gsub("Controller", "").singularize.constantize

But is there a built in method for this in the controller?

Something like self.model_class?

Thanks开发者_运维百科!


params[:controller].classify.constantize


ActionPack and ActiveRecord are separate modules, so there's no built-in support for that.


Another way:

controller_name.classify
0

精彩评论

暂无评论...
验证码 换一张
取 消