devise + omniauth设计帮助器,如current_user,user_signed_in?不工作

urj*_*ils 0 devise omniauth ruby-on-rails-3

我使用omniauth使用设计并使用Facebook创建登录,但是有丢失设计帮助方法访问的问题,如current_user和user_signed_in?方法不起作用.

编辑

AuthenticationController

def create
    omniauth = request.env["omniauth.auth"]    
    user = User.find_by_provider_and_uid(omniauth["provider"], omniauth["uid"]) ||       User.create_with_omniauth(omniauth)    
    session[:user_id] = user.id    
    redirect_to dashboard_path(user.id), :notice => "Signed in!"    
end  
Run Code Online (Sandbox Code Playgroud)

redirect_to USercontroller仪表板方法

UserController的

before_filter  :logged_in

 def dashboard    
    @user = User.find(params[:id])   
    @comment = Comment.new    
    @comments = @user.comments.all.paginate(:page => params[:page], :per_page => 5)    
 end 
Run Code Online (Sandbox Code Playgroud)

所以这里控制应该在检查ApplicationController中的logged_in方法后转到dashboard方法

ApplicationController中的logged_in方法

应用控制器

def logged_in    
    if user_signed_in?     
       return true    
    else    
       redirect_to root_path    
       flash[:message] = "please login"    
    end     
  end 
Run Code Online (Sandbox Code Playgroud)

当我使用Facebook在控制台生成的代码后登录时

Started GET "/users/52/dashboard" for 127.0.0.1 at Thu Mar 29 12:51:55 +0530 2012     
Processing by UsersController#dashboard as HTML     
  Parameters: {"id"=>"52"}     
Redirected to http://localhost:3000/     
Filter chain halted as :logged_in rendered or redirected     
Completed 302 Found in 2ms (ActiveRecord: 0.0ms)     
Run Code Online (Sandbox Code Playgroud)

在上面的代码控件中,从logged_in方法渲染到root_path,但是它展示了dashboard_path

所以我猜猜User_signed_in?帮助器不工作我也使用current_user代替生成相同的错误

alo*_*ony 6

正如我所见,user_signed_in?正在工作,但返回false,因为Devise用户没有登录.要修复此问题,只需用sign_in控制器操作中的Devise 方法替换存储的会话ID :

def create
    omniauth = request.env["omniauth.auth"]    
    user = User.find_by_provider_and_uid(omniauth["provider"], omniauth["uid"]) ||       User.create_with_omniauth(omniauth)    
    sign_in(:user, user)

    # actually if you really really need that id in the session, you can leave this line too :)
    session[:user_id] = user.id 

    redirect_to dashboard_path(user.id), :notice => "Signed in!"    
end 
Run Code Online (Sandbox Code Playgroud)