Rails 3 w / Devise:如何根据用户的身份验证来设置两个独立的主页面? [英] Rails 3 w/ Devise: How to set two separate homepages based on whether the user is authenticated or not?

查看:92
本文介绍了Rails 3 w / Devise:如何根据用户的身份验证来设置两个独立的主页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Rails 3和Devise创建一个用户到达网站的应用程序,并显示一个包含登录名和注册表单的主页。这个页面有自己的控制器(主页),所以路由是

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"

有没有办法在routes.rb中有条件路由,允许我检查用户是否被认证,然后将它们路由到这些主页之一?

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"

此外,我希望url指向www.example.com案例,以便www.example.com/dashboard/index和www.example.com/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.

感谢一百万!

推荐答案

尽管如此,它是专门针对Warden / Devise的。

Try this, it's specific to Warden/Devise though.

root to: "dashboard#index", constraints: lambda { |r| r.env["warden"].authenticate? }
root to: "homepage#index"

这篇关于Rails 3 w / Devise:如何根据用户的身份验证来设置两个独立的主页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆