I am using Ruby on Rails 3.0.7 and I am trying to set correctly (and as well as possible) my router.
In the routes.rb file I have:
namespace :articles do
resources :categories
end
resources :articles
In order to work as expected, I must state the namespace before the resources :articles statement so that the router intercepts requests and direct these to the namespace related to articles. Otherwise (if the resources :articles is stated before of the namespace), as it is possible deduct from the above code, all requests like
<my_site>/articles/1
<my_site>/articles/new
<my_site>/articles/1/edit
...
are intercepted so that the articles namespace will be “hardly” reached. For example, if you make a request like <my_site>/articles/categories it will generate an error as the following:
ActiveRecord::RecordNotFound
Couldn't find Article with ID=categories
So, how can I handle this situation and how can I improve the router code? What do you advice about?
Running the command rake routes in my console I get this (as it is ordered):
articles_categories GET /articles/categories(.:format)
{:action=>"index", :controller=>"articles/categories"}
POST /articles/categories(.:format)
{:action=>"create", :controller=>"articles/categories"}
new_articles_category GET /articles/categories/new(.:format)
{:action=>"new", :controller=>"articles/categories"}
edit_articles_category GET /articles/categories/:id/edit(.:format)
{:action=>"edit", :controller=>"articles/categories"}
articles_category GET /articles/categories/:id(.:format)
{:action=>"show", :controller=>"articles/categories"}
PUT /articles/categories/:id(.:format)
{:action=>"update", :controller=>"articles/categories"}
DELETE /articles/categories/:id(.:format)
{:action=>"destroy", :controller=>"articles/categories"}
articles GET /articles(.:format)
{:action=>"index", :controller=>"articles"}
POST /articles(.:format)
{:action=>"create", :controller=>"articles"}
new_article GET /articles/new(.:format)
{:action=>"new", :controller=>"articles"}
edit_article GET /articles/:id/edit(.:format)
{:action=>"edit", :controller=>"articles"}
article GET /articles/:id(.:format)
{:action=>"show", :controller=>"articles"}
PUT /articles/:id(.:format)
{:action=>"update", :controller=>"articles"}
DELETE /articles/:id(.:format)
{:action=>"destroy", :controller=>"articles"}
Check this rails documentation about controller namespaces. It says you need to namespace your categories controller like
Articles::CategoriesControllerand you need to place your categories controller underapp/controllers/articlesdirectory.