hyphen resources in rails 3 routes - ruby-on-rails

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

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.

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

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

Related

Change resource route name

Hou could I change resource name without changing resource URL.
Explanation:
My resource route is resources :universities
So my routes are new_university_path or universities_path
Now I have changed my resource name to departments as follows:
resources :departments, :controller => :universities
But problem is when I run rake routes I found that my URL has already been changed. So I like a solution where after changing my resource name my routes are works as before, like new_university_path or universities_path
Is this possible in rails 3 ?
You can add:
resources :departments, :controller => :universities, :as => :universities
That should keep the URL helpers the same.
You should be able to do that by using naming routes by giving it the name you want for that route in :as option:
resources :departments, :controller => :universities, :as => :universities

edit_content_path wrong Ruby On Rails

Hi!
I have many links like <%= link_to 'Edit', edit_content_path(content) %> but since my Rails update and some other route changes the path is wrong.
It leads to domain/contents instead of the correct domain/subfolder/contents... what is wrong?
Thanks!
Have you nested any routes in your routes.rb file?
If you you will have to add the parent to the path.
edit_parent_content_path(#parent, #content_id)
Something like that. Running ->
$ rake routes
Will help you determine if it is a route problem. (Try to find your current path in the results.)
If you still have a problem, post your routes.rb file.
Here is my routes.rb:
ActionController::Routing::Routes.draw do |map|
map.root :controller => "store"
map.resources :users
map.resources :orders, :controller => 'order'
map.resources :contents
map.resources :products
map.resources :kundservice => "store/kundservice"
map.resources :foretaget => "store/foretaget"
# Install the default routes as the lowest priority.
map.connect 'subfolder/:controller/:action/:id'
map.connect 'subfolder/:controller/:action/:id.:format'
end
Because of the hostcompany where I have my site, I must have it in a subfolder under my domain, www.domain.com/subfolder/store.
Thanks!

Difference between :as option in Rails 2 and Rails3 routing?

In Rails 2.X we have:
map.resources :posts, :controller => 'posts', :as => 'articles'
This essentially creates an alias for our posts routes. For example, this sends "domain.com/articles/" to the posts controller index action.
In Rails3, however, the :as option behaves differently. For example:
resources :posts, :controller => 'posts', :as => 'articles'
sets a named route rather than an alias, and going to "domain.com/articles/" gives an error:
No route matches {:controller=>"posts"}
How do I get the old (Rails 2) :as behavior using the new (Rails 3) api?
PS: Please don't tell me to simply rename my controller. That's not an option for me.
From some cursory reading of the RoR guide on routing, I think you might need to try:
resources :articles, :controller => "posts"
(http://guides.rubyonrails.org/routing.html#specifying-a-controller-to-use)
You might also need to add :as => "articles", but that named helper might already be set up since you are adding :articles resources.
You can accomplish this same behavior using the path option:
resources :posts, :path => '/articles/'
Now for example /posts/new becomes /articles/new.

Creating Twitter-style routes with Rails3

How can I make Twitter-style routes with Rails3?
I've tried the following:
match ':username', :controller => "users", :action => "show"
match ':username/:controller(/:action(/:id))', :path_prefix => '/:username'
EDIT
After some more digging through the docs, I did this and it seems to work:
scope '/:username' do
resources :clubs
end
What is the "scope" method exactly and is there an automatic way of generating link_to URLs in my views?
The following matcher will match /dhh/update/1
match ':username/update/:id' => 'updates#show'
'update#show' is new in Rails 3 and is the short version of :controller => 'updates', :action => 'show'
Try clubs_path(:username => 'bob').

Giving a nested route an alias in Rails

If I want to provide an alias for a controller, I can use map.resources :rants, :controller => 'blog_posts' yoursite.com/rants points to the blog_posts controller fine.
How do I give an alias to a nested resource, for example yoursite.com/users/5/rants ?
You may want to try:
map.resources :rants, :controller => 'blog_posts'
map.resources :users do |users|
users.resources :rants, :controller => 'blog_posts'
end
This will give you the yoursite.com/users/5/rants/ url that you are looking for and it will generate the handy methods (for example: users_rants_path(#user))
Hope this helps.

Resources