Rails 3 w/ Devise:如何根据用户是否经过身份验证设置两个单独的主页?

发布于 2024-12-27 08:53:33 字数 631 浏览 4 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

半城柳色半声笛 2025-01-03 08:53:33

试试这个,它是特定于守望者/设计的。

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

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

root to: "dashboard#index", constraints: lambda { |r| r.env["warden"].authenticate? }
root to: "homepage#index"
吻安 2025-01-03 08:53:33

在你的 HomeController 中:

def index
  if !user_signed_in?
    redirect_to :controller=>'dashboard', :action => 'index'
  end
end

In your HomeController:

def index
  if !user_signed_in?
    redirect_to :controller=>'dashboard', :action => 'index'
  end
end
蓝眼泪 2025-01-03 08:53:33

(这里回答了完全相同的问题:https://stackoverflow.com/a/16233831/930038。也在这里添加答案供其他人参考。)

在您的 routes.rb 中:

authenticated do
  root :to => 'dashboard#index'
end

root :to => 'homepage#index'

这将确保所有经过身份验证的用户的 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 :

authenticated do
  root :to => 'dashboard#index'
end

root :to => 'homepage#index'

This will ensure that root_url for all authenticated users is dashboard#index

For your reference: https://github.com/plataformatec/devise/pull/1147

对你的占有欲 2025-01-03 08:53:33

这是 Rails 4 的正确答案,

root to: 'dashboard#index', constraints: -> (r) { r.env["warden"].authenticate? },
         as: :authenticated_root
root to: 'homepage#index'

我尝试将其添加到/编辑已接受的答案,但显然它的编辑量太大,无法接受。不管怎样,投票给接受的答案(来自布拉德利),它帮助我想出了这个:)

Here's the correct answer with rails 4

root to: 'dashboard#index', constraints: -> (r) { r.env["warden"].authenticate? },
         as: :authenticated_root
root to: 'homepage#index'

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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文