如果我按原样使用下面的代码(dashboards#show
),我找不到错误Couldn't find Dashboard without an ID
的记录,因为我在路径中没有用户ID(我通常链接到此使用此仪表板:user_dashboard_path(current_user, current_user.dashboard)
< - 不能在routes.rb中使用它。
我想我需要将用户传递到设置根路径的代码块,但我不确定如何实现。
任何提示?
这是我的所有routes.rb文件:
MyApp::Application.routes.draw do
resources :users do
resources :dashboards, only: [:show, :edit, :update, :destroy]
end
authenticated :user do
# Instead of the show action, I want to go to user_dashboard which is
# GET /users/:user_id/dashboards/:id(.:format) dashboards#show
root :to => "dashboards#show", as: :authenticated_root
end
root :to => "home#index"
devise_for :users, :controllers => { :registrations => :registrations }
end
答案 0 :(得分:2)
选项1:
在仪表板控制器中,切换到:
@user = current_user
@dashboard = current_user.dashboard
这也会阻止用户看到其他用户的信息中心。
选项2:
现在,如果用户可以正常查看其他用户的信息中心,请更改您的路线:
root :to => "dashboards#find", as: :authenticated_root
并在仪表板控制器中:
def find
redirect_to user_dashboard_path(current_user, current_user.dashboard)
end