Rails 3 w/ Devise:如何根据用户是否经过身份验证设置两个单独的主页?
我正在使用 Rails 3 和 Devise 创建一个应用程序,用户可以在该应用程序中访问该网站,并看到一个包含登录名和注册表单的主页。该页面有自己的控制器(“主页”),因此它的路线是
root :to => "homepage#index"
如果用户已经登录,我想显示不同的主页。这将说明根指向
root :to => "dashboard#index"
是否有办法在路线中拥有条件路线.rb,这将允许我在将用户路由到这些主页之一之前检查用户是否经过身份验证?
我尝试使用以下代码,但如果我没有登录,devise 会要求我登录,所以显然只有第一条路线有效。
authenticate :user do
root :to => "dashboard#index"
end
root :to => "homepage#index"
另外,我希望在这两种情况下 url 都指向 www.example.com,以便 www.example.com/dashboard/index 和 www.example.com/homepage/index 永远不会出现在浏览器中。
谢谢一百万!
I am using Rails 3 and Devise to create an app where users arrive to the website and are shown a homepage containing a login and a signup form. This page has its own controller ("homepage") so it's route is
root :to => "homepage#index"
I want to display a different homepage if the users are already logged in. This would account to having the root point to
root :to => "dashboard#index"
Is there a way to have a conditional route in routes.rb, that would allow me to check whether the user is authenticated before routing them to one of those homepages?
I tried using the following code but if I'm not logged in, devise asks me to log in, so clearly only the first route works.
authenticate :user do
root :to => "dashboard#index"
end
root :to => "homepage#index"
Also, I want the url to point to www.example.com in both cases, so that www.example.com/dashboard/index and www.example.com/homepage/index never appear in the browser.
Thanks a million !!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
试试这个,它是特定于守望者/设计的。
Try this, it's specific to Warden/Devise though.
在你的 HomeController 中:
In your HomeController:
(这里回答了完全相同的问题:https://stackoverflow.com/a/16233831/930038。也在这里添加答案供其他人参考。)
在您的
routes.rb
中:这将确保所有经过身份验证的用户的
root_url
dashboard#index
供您参考:https://github.com/ plataformatec/devise/pull/1147
(Exact same question answered here: https://stackoverflow.com/a/16233831/930038. Adding the answer here too for others' reference.)
In your
routes.rb
:This will ensure that
root_url
for all authenticated users isdashboard#index
For your reference: https://github.com/plataformatec/devise/pull/1147
这是 Rails 4 的正确答案,
我尝试将其添加到/编辑已接受的答案,但显然它的编辑量太大,无法接受。不管怎样,投票给接受的答案(来自布拉德利),它帮助我想出了这个:)
Here's the correct answer with rails 4
I tried to add this to / edit the accepted answer but it's too much of an edit to be accepted apparently. Anyway, vote for the accepted answer (from Bradley), it helped me come up with this one :)