I want my urls to use dash -
instead of underscore _
as word separators. For example controller/my-action
instead of controller/my_action
.
I'm surprised about two things:
-
to _
in the routing. Or does it? The best solution I've is to use :as
or a named route.
My idea is to modify the Rails routing to check for that global config and change -
to _
before dispatching to a controller action.
Is there a better way?
With Rails 3 and later you can do like this:
resources :user_bundles, :path => '/user-bundles'
Another option is to modify Rails, via an initializer. I don't recommend this though, since it may break in future versions (edit: doesn't work in Rails 5).
Using :path
as shown above is better.
# Using private APIs is not recommended and may break in future Rails versions.
# https://github.com/rails/rails/blob/4-1-stable/actionpack/lib/action_dispatch/routing/mapper.rb#L1012
#
# config/initializers/adjust-route-paths.rb
module ActionDispatch
module Routing
class Mapper
module Resources
class Resource
def path
@path.dasherize
end
end
end
end
end
end