Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails namespaced routes work in development but not production

I'm trying to nest some routes under the namespace, account.

I want user management under account like /account/users and /account/users/5/edit

In routes.rb:

namespace :account do
  resources :users do
    member do
      put 'generate_api_key'
    end 

    collection do
      post 'api_key'
    end 
  end 
end 

My controllers are not namespaced or put them in any different directory.

/app
  /controllers
    accounts_controller.rb
    users_controller.rb

In my development environment this is working fine, but in production I get 404 responses to any of the /account/users... paths (which, by the way, are all still generated correctly: new_account_users_path, edit_account_user_path, etc).

rake routes generates the same output in both environments. Here is the relevant bit:

 generate_api_key_account_user PUT    /account/users/:id/generate_api_key(.:format)                      {:action=>"generate_api_key", :controller=>"account/users"}
         api_key_account_users POST   /account/users/api_key(.:format)                                   {:action=>"api_key", :controller=>"account/users"}
                 account_users GET    /account/users(.:format)                                           {:action=>"index", :controller=>"account/users"}
                               POST   /account/users(.:format)                                           {:action=>"create", :controller=>"account/users"}
              new_account_user GET    /account/users/new(.:format)                                       {:action=>"new", :controller=>"account/users"}
             edit_account_user GET    /account/users/:id/edit(.:format)                                  {:action=>"edit", :controller=>"account/users"}
                  account_user GET    /account/users/:id(.:format)                                       {:action=>"show", :controller=>"account/users"}
                               PUT    /account/users/:id(.:format)                                       {:action=>"update", :controller=>"account/users"}
                               DELETE /account/users/:id(.:format)                                       {:action=>"destroy", :controller=>"account/users"}

Given that the routes seem to look for the Users controller in the /account subdirectory, I suppose my question is why does this work in development?

Production is:

  • Rails 3.0.7
  • Passenger
  • Apache

Development is:

  • Rails 3.0.7
  • Mongrel

Thanks for your thoughts on this one.

like image 394
doctororange Avatar asked May 31 '11 03:05

doctororange


1 Answers

If you're namespacing like this, Rails requires the controllers to be at their correct paths, such as app/controllers/account/users_controller.rb. If you don't want to do this, then use scope instead:

scope :path => "account" do
  resources :users
end
like image 108
Ryan Bigg Avatar answered Oct 19 '22 05:10

Ryan Bigg