在Rails 4中路由具有多个单词的控制器 [英] Routing for controllers with multiple words in in Rails 4
问题描述
我刚升级到Rails 4,发现路由异常。
我们有一个名为EmailPreviewController的控制器。路由如下:
获取 / emailpreview,控制器: EmailPreview,操作:: index
但是,升级到Rails 4后,在加载环境时会引发以下错误:
'EmailPreview'不是受支持的控制器名称。这可能导致潜在的路由问题。请参阅http://guides.rubyonrails.org/routing.html#specifying-a-controller-to-use
我已经查看了页面建议,但是没有任何迹象表明使用带有CamelCase名称的控制器是不正确的。
如果我将控制器更改为小写字母,则没有任何问题:
#可以正常工作
get / emailpreview,控制器:'emailpreview',操作::index
这是预期的行为吗?
答案是某种程度上,这是不可能的吗?
违反直觉的。我认为它是设计好的,但是这不是我期望的。
在Rails 3中,您可以使用对象的名称指定控制器
在Rails 3中,您可以传递控制器对象的名称,Rails会找到它的方式:
获取 emailpreview,控制器: EmailPreview,操作:: index
将找到其到 email_preview.rb
中包含的 EmailPreviewController
的方式。
但是在Rails 4中,您需要在蛇形情况下传递控制器名称
在Rails 4中,您似乎需要在蛇形情况下传递控制器对象的名称:
获取 emailpreview,控制器: email_preview,操作:: index
这将进入 email_preview中包含的
。 EmailPreviewController
。 rb
另请参见 http:// guides .rubyonrails.org / routing.html#controller-namespaces-and-routing (尽管在这种情况下并不能解释太多)
I've just upgraded to Rails 4 and found an unexpected behaviour with routing.
We have a controller named EmailPreviewController. The routing for this was:
get "/emailpreview", controller: 'EmailPreview', action: :index
however after upgrading to Rails 4 this throws the following error when the environment is loaded:
'EmailPreview' is not a supported controller name. This can lead to potential routing problems. See http://guides.rubyonrails.org/routing.html#specifying-a-controller-to-use
I've looked at the page it suggests however there isn't any indication that it's incorrect to use a controller with a CamelCase name.
If I change the controller to lower case there isn't any problem:
# this works fine
get "/emailpreview", controller: 'emailpreview', action: :index
Is this expected behaviour? Is it not possible to use camelcase controller names now or is there something else going on here?
The answer to this was somewhat counter-intuitive. I assume it's as-designed but it's not what I would have expected.
In Rails 3, you could specify controllers using the object's name
In Rails 3, you could pass the name of the controller object and Rails would find its way to it:
get "emailpreview", controller: 'EmailPreview', action: :index
would find its way to the EmailPreviewController
contained within email_preview.rb
.
however in Rails 4 you need to pass controller names in snake-case
In Rails 4 it seems that you need to pass the name of the controller object in snake-case:
get "emailpreview", controller: 'email_preview', action: :index
This will make its way to the EmailPreviewController
contained within email_preview.rb
.
Also see http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing (though this doesn't explain much in this particular instance)
这篇关于在Rails 4中路由具有多个单词的控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!