如何重新使用登录页面的根路径并将其作为登录用户的默认路径?

时间:2014-03-09 18:22:05

标签: ruby-on-rails-4 devise rails-routing

我希望 foo.com / 在用户未登录时显示登录页面, foo.com / 显示用户的信息中心时用户已登录,就像在Facebook中一样。

我打算按照以下方式去做:

def index
  if user_signed_in?
    @posts = current_user.posts
    render 'home/dashboard'
  else
    render 'home/landing'
  end
end

然后很快意识到我需要使用before_filter :authenticate_user!才能获得current_user,因此Devise将需要登录。

也许有一种方法可以更清晰地使用路由来做到这一点。请指教。

1 个答案:

答案 0 :(得分:1)

authenticated :user do
  root to: "users#index", as: :authenticated_root
end

unauthenticated do
  root to: "main#index"
end

https://github.com/plataformatec/devise/issues/2393#issuecomment-17298414

相关问题