Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails controller names with dashes instead of underscores

Is it possible to forces Rails to use dashes (-) instead of underscores when referring to controllers.

There currently exists a nice function of the Inflector called parameterize. It allows for very nice permalinks with all special characters removed and replaced with dashes...

However, when using controllers that have multiple words (like contact_methods_controller.rb for example), you define your route:

resources :contact_methods

This creates a map to /contact_methods (NOT /contact-methods). When I mix these two, I get ugly URLs like:

/contact_methods/1-preferred-email

I'd like to have Rails map controllers with dashes instead of underscores. All my research says to individually map each controller:

match 'contact-methods(/:action)' => 'contact_methods'

but that is really stupid, in my opinion, and it becomes messy if I'm nesting resources... I shouldn't have to define these as custom routes. Is there a setting in ActionDispatch that automatically rewrites these things? I can't find one...

like image 320
sethvargo Avatar asked Feb 03 '23 20:02

sethvargo


1 Answers

In your route.rb

resources "contact-methods", :controller => :contact_methods, :as => :contact_methods

Edit: You have to specify the :as => ... path or else ActionDispatch throws a fit...

like image 194
fl00r Avatar answered Feb 05 '23 16:02

fl00r