Pet*_*xey 8 routing ruby-on-rails-4
我刚刚升级到Rails 4并发现了路由的意外行为.
我们有一个名为EmailPreviewController的控制器.这个路由是:
get "/emailpreview", controller: 'EmailPreview', action: :index
Run Code Online (Sandbox Code Playgroud)
但是在升级到Rails 4之后,在加载环境时会抛出以下错误:
'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
Run Code Online (Sandbox Code Playgroud)
我查看了它建议的页面但是没有任何迹象表明使用具有CamelCase名称的控制器是不正确的.
如果我将控制器更改为小写,则没有任何问题:
# this works fine
get "/emailpreview", controller: 'emailpreview', action: :index
Run Code Online (Sandbox Code Playgroud)
这是预期的行为吗?是不是现在可以使用camelcase控制器名称,还是有其他东西在这里?
Pet*_*xey 14
对此的回答有些违反直觉.我认为它是按照设计的,但它不是我所期望的.
在Rails 3中,您可以传递控制器对象的名称,Rails会找到它的方式:
get "emailpreview", controller: 'EmailPreview', action: :index
Run Code Online (Sandbox Code Playgroud)
会找到EmailPreviewController包含在内的方式email_preview.rb.
在Rails 4中,似乎需要以snake-case传递控制器对象的名称:
get "emailpreview", controller: 'email_preview', action: :index
Run Code Online (Sandbox Code Playgroud)
这将进入EmailPreviewController内部email_preview.rb.
另请参阅http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing(尽管在此特定实例中没有解释太多)