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)
.
Asked
Active
Viewed 2,343 times
10

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 Answers
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
-
1I think this works too (difference would be in the naming of route helpers): `resources :product_types, :path => '/product-types'` – sandstrom Oct 17 '11 at 18:49
-
3It works without the **controller** part with the latest Rails 3 versions. – Timo Saloranta Mar 27 '13 at 14:38
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
-
I have already tred to do this. I get the same error as when I am using hyphen-named model. – Andrey Kuznetsov Nov 03 '10 at 02:08
-
-
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
-
Yes it is obvious solution but It is interesting how is it possible to define hyphen routes using resources. – Andrey Kuznetsov Nov 02 '10 at 15:55