Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Routing for controllers with multiple words in in Rails 4

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?

like image 538
Peter Nixey Avatar asked Oct 26 '13 13:10

Peter Nixey


1 Answers

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)

like image 193
Peter Nixey Avatar answered Oct 24 '22 05:10

Peter Nixey