I'm trying to create a Rails app that divides the users by groups and sub-groups and offers a customized site to each sub-group. I figured the easiest way to do that was to create scope with named parameters:
scope ":group/:subgroup/" do
devise_for :users
resources :users
end
"rake routes" then produces this:
users GET /:group/:subgroup/users(.:format) {:action=>"index", :controller=>"users"}
POST /:group/:subgroup/users(.:format) {:action=>"create", :controller=>"users"}
new_user GET /:group/:subgroup/users/new(.:format) {:action=>"new", :controller=>"users"}
edit_user GET /:group/:subgroup/users/:id/edit(.:format) {:action=>"edit", :controller=>"users"}
user GET /:group/:subgroup/users/:id(.:format) {:action=>"show", :controller=>"users"}
PUT /:group/:subgroup/users/:id(.:format) {:action=>"update", :controller=>"users"}
DELETE /开发者_如何转开发:group/:subgroup/users/:id(.:format) {:action=>"destroy", :controller=>"users"}
It works perfectly--except when I to use link_to:
link_to current_user.name, current_user
Then it throws this error
ActionController::RoutingError in Users#index
Showing C:/Rails/MyApp/app/views/users/_my_list.html.haml where line #8 raised:
No route matches {:action=>"show", :controller=>"users", :group=>#<User id: 7, email: "username@domain.com", encrypted_password: "$2a$10$GdZeC0b4VaNxdsDXP...", password_salt: "$2a$sj$88gm0nttYE.7a4IHi.BNO", reset_password_token: nil, remember_token: nil, remember_created_at: nil, sign_in_count: 86, current_sign_in_at: "2011-08-05 17:18:19", last_sign_in_at: "2011-08-05 00:14:26", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", confirmation_token: nil, confirmed_at: "2010-12-09 23:08:54", confirmation_sent_at: "2010-12-09 23:08:36", failed_attempts: 0, unlock_token: nil, locked_at: nil, created_at: "2010-12-09 23:08:36", updated_at: "2011-08-05 17:18:19", name: "UserOne">}
Line #8, of course, is where I tried to use link_to. My guess is that link_to is ignoring the scope, which accounts for current_user getting stuffed into the :group parameter. Is there a way to keep this from happening, or am I going about this whole issue totally wrong?
Your resource :users
is inside the scope, so you don't have the /users/:id
route.
You could try this:
scope ":group/:subgroup/" do
devise_for :users
end
resources :users
I'm not sure if the scope
has a shallow => true
option (I think it does and you should probably use it).
Hope that helps
The problem is you can't reach an user without passing in a group and a subgroup. Reading your error it's possible to understand that rails is trying to convert your current_user to the group.
精彩评论