AdamNYC
AdamNYC

Reputation: 20415

Rails routes map incorrectly

I have the following statement in routes:

  resource :users, :only => [:index, :show, :update, :destroy] do
    get "users/dashboard"
  end

However, when I do rake routes, I got:

dashboard_users GET    /users/users/dashboard(.:format)       users/users#dashboard
               users GET    /users(.:format)                       users#show
                     PUT    /users(.:format)                       users#update
                     DELETE /users(.:format)                       users#destroy

Note that users_path was incorrectly mapped to users#show. I was expecting:

users GET    /users/:id/(.:format)                       users#show

What might cause this issue and how I can fix it?

Upvotes: 0

Views: 35

Answers (2)

sp1rs
sp1rs

Reputation: 786

Instead of doing this

resource :users, :only => [:index, :show, :update, :destroy] do
  get "users/dashboard"
end

Do this

namespace :dashboard do 
    resource :users, :only=>[:index,:show,:udpate,:destroy]
end

I think this will work for you.

Upvotes: 0

Siwei
Siwei

Reputation: 21569

you should use resources instead of resource. e.g.

resources 'users' 

the difference between resource and resources is:

resource is for operations for a single model. when using resource model, there's no "index" action.

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

further more, I notice that your route is a bit strange. if you just want to add an action named 'dashboard' with GET accessing method, just declare like this:

resources 'users' do 
  get :dashboard
end

Upvotes: 1

Related Questions