Rails 4 + 设计:为经过身份验证的用户设置默认根路由
Rails 4 + Devise: set default root route for authenticated users
我做了一些研究,发现这个问题已经在不同的地方多次解决:
- Devise with rails 4 authenticated root route not working
- Different '/' root path for users depending if they are authenticated (using devise)
- Authenticated Route Constraints
- Rails Tip #5: Authenticated Root and Dashboard Routes With Devise
我尝试按照上面链接中给出的示例进行操作,并在我的 routes.rb
文件中提出了以下解决方案:
root to: 'pages#home'
devise_for :users, :path => 'account', controllers: { registrations: "registrations", confirmations: "confirmations" }
authenticated :user do
root 'calendars#index', as: :authenticated_root
end
此处的目标是将 unauthenticated
用户引导至网站的常规 home
页面,而 authenticated
用户将转到应用程序内部的仪表板,即 calendars#index
路由,我的routes中定义如下:
calendars GET /calendars(.:format) calendars#index
但是,当我以用户身份登录并访问 http://localhost:3000/
时,我一直登陆网站的常规 home
页面,而不是应用程序内的用户仪表板。
我做错了什么?
更改 routes.rb
以便包装未经身份验证的 root
路由,就像经过身份验证的路由一样:
devise_for :users, :path => 'account', controllers: { registrations: "registrations", confirmations: "confirmations" }
authenticated :user do
root 'calendars#index', as: :authenticated_root
end
unauthenticated :user do
root 'pages#home', as: :unauthenticated_root
end
我做了一些研究,发现这个问题已经在不同的地方多次解决:
- Devise with rails 4 authenticated root route not working
- Different '/' root path for users depending if they are authenticated (using devise)
- Authenticated Route Constraints
- Rails Tip #5: Authenticated Root and Dashboard Routes With Devise
我尝试按照上面链接中给出的示例进行操作,并在我的 routes.rb
文件中提出了以下解决方案:
root to: 'pages#home'
devise_for :users, :path => 'account', controllers: { registrations: "registrations", confirmations: "confirmations" }
authenticated :user do
root 'calendars#index', as: :authenticated_root
end
此处的目标是将 unauthenticated
用户引导至网站的常规 home
页面,而 authenticated
用户将转到应用程序内部的仪表板,即 calendars#index
路由,我的routes中定义如下:
calendars GET /calendars(.:format) calendars#index
但是,当我以用户身份登录并访问 http://localhost:3000/
时,我一直登陆网站的常规 home
页面,而不是应用程序内的用户仪表板。
我做错了什么?
更改 routes.rb
以便包装未经身份验证的 root
路由,就像经过身份验证的路由一样:
devise_for :users, :path => 'account', controllers: { registrations: "registrations", confirmations: "confirmations" }
authenticated :user do
root 'calendars#index', as: :authenticated_root
end
unauthenticated :user do
root 'pages#home', as: :unauthenticated_root
end