问题
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?
回答1:
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)
来源:https://stackoverflow.com/questions/19607305/routing-for-controllers-with-multiple-words-in-in-rails-4