Rails - Routes

rails

http://guides.rubyonrails.org/routing.html

What does the Rails community think about using resource objects instead of declaring routes manually?

Rails encourages the use of resources objects instead of declaring routes manually. See http://guides.rubyonrails.org/getting_started.html.

How to add a route for a resource?

As with the welcome controller, we will need to add a route so that Rails knows where we would like to navigate to see comments. Open up the config/routes.rb file again, and edit it as follows:

resources :articles do
  resources :comments
end

This creates comments as a nested resource within articles. This is another part of capturing the hierarchical relationship that exists between articles and comments.

How to add a route for a resource that is nested within another resource?

As with the welcome controller, we will need to add a route so that Rails knows where we would like to navigate to see comments. Open up the config/routes.rb file again, and edit it as follows:

resources :articles do
  resources :comments
end

This creates comments as a nested resource within articles. This is another part of capturing the hierarchical relationship that exists between articles and comments.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License