10

How is it possible to use hyphen in resources urls? For example: /my-model/ or /my-model/1. If I define route as resources :"my-model" I get syntax error because rails generates method def hash_for_my-models_url(options = nil).

Andrey Kuznetsov
  • 11,640
  • 9
  • 47
  • 70
  • possible duplicate of [In Ruby on Rails Routing I Would Like to Use Dash \`-\` Instead of Underscore \`\_\`](http://stackoverflow.com/questions/5334465/in-ruby-on-rails-routing-i-would-like-to-use-dash-instead-of-underscore) – Brad Werth Apr 03 '15 at 21:26

3 Answers3

14

I have found the solution:

  resources "my-models", :as => :my_models, :controller => :my_models

UPDATE: As Timo Saloranta said in comment it works without :controller => :my_models in latest Rails 3 versions.

Andrey Kuznetsov
  • 11,640
  • 9
  • 47
  • 70
2

You can use the :as option to configure resourceful routes with hyphenated URLs:

map.resources :my_model, :as => "my-model"

results in

my_model_index GET /my-model(.:format) {:action=>"index",
  :controller=>"my_model"}

...etc...

zetetic
  • 47,184
  • 10
  • 111
  • 119
0

Have you tried a custom route?

map.connect "/my-model/:id", :controller => 'my-model-controller', :action => 'read'

This would invoke the 'read' method of 'my-model-controller.rb'.

nrj
  • 1,701
  • 2
  • 22
  • 37