我创建了一个登录控制器,以便人们可以使用Facebook登录。当用户登录时,他会被路由到frontpage/show
,当他们注销时,他们会被重定向到root_url并显示登录页面。
问题在于,当用户通过Facebook登录,关闭网站然后重新访问该页面时,他也会被定向到root_url,而不是frontpage/show
。
我为此使用了Omniauth和Rails。
这是我的session_controller.rb
class SessionsController < ApplicationController
def create
user = User.from_omniauth(env["omniauth.auth"])
session[:user_id] = user.id
redirect_to '/frontpage/show'
end
def destroy
session[:user_id] = nil
redirect_to root_url
end
end
和我的frontpage_controller.rb
class FrontpageController < ApplicationController
def show
end
end
我尝试过添加
def show
if authenticate_user?
redirect_to :controller=>'dashboard', :action => 'index'
else
redirect_to '/public/example_html_file.html'
end
end
到frontpage_controller,但是它提供了错误ndefined method
authenticate_user`。但我相信这样的事情就是答案。
答案 0 :(得分:0)
好吧,我假设你正在使用Devise
。因此,您使用方法authenticate_user!
而不是authenticate_user?
。
现在,要检查用户是否已登录,您可以使用user_signed_in?
代替authenticate_user?
,您的代码应该可以使用。
有关这些辅助方法的更多信息https://github.com/plataformatec/devise#controller-filters-and-helpers