开发者

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...


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...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜