Rails 3:命名参数路由中断 link_to
我正在尝试创建一个 Rails 应用程序,该应用程序按组和子组划分用户,并为每个子组提供自定义网站。我认为最简单的方法是使用命名参数创建作用域:
scope ":group/:subgroup/" do
devise_for :users
resources :users
end
“rake paths”然后产生这样的结果:
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"}
它工作得很好——除非我使用 link_to:
link_to current_user.name, current_user
然后它会抛出这个错误
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: "[email protected]", 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">}
第 #8 行,当然,是在哪里我尝试使用 link_to。我的猜测是 link_to 忽略了作用域,这导致 current_user 被塞进 :group 参数中。有没有办法阻止这种情况发生,或者我对整个问题的看法完全错误?
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: "[email protected]", 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的
resource :users
在范围内,因此您没有/users/:id
路由。您可以尝试以下操作:
我不确定
scope
是否有shallow =>; true
选项(我认为确实如此,您可能应该使用它)。希望有帮助
Your
resource :users
is inside the scope, so you don't have the/users/:id
route.You could try this:
I'm not sure if the
scope
has ashallow => true
option (I think it does and you should probably use it).Hope that helps
问题是如果不传递组和子组就无法联系用户。阅读您的错误,可以理解 Rails 正在尝试将您的 current_user 转换为该组。
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.