2015-08-27 41 views
0

这就是我的OmniauthCallbacksController的样子。我为这里的FB登录设置会话[:user_id],它工作正常。为什么在使用设计注册后会话[:user_id]为空?

class SessionsController < Devise::OmniauthCallbacksController 
    def facebook 
    @user = User.from_omniauth(request.env["omniauth.auth"]) 

    if @user.persisted? 
     session[:user_id] = @user.id 
     line #9 
     sign_in_and_redirect @user , :event => :authentication #this will throw if @user is not activated 
     set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format? 
    else 
     session["devise.facebook_data"] = request.env["omniauth.auth"] 
     redirect_to new_user_registration_url 
    end 
    end 

    def destroy 
    session[:user_id] = nil 
    redirect_to root_url 
    end 
end 

class ApplicationController < ActionController::Base 

    protect_from_forgery with: :exception 



    # uncomment this block if I want to set session after a sign up through devise. 
    # def after_sign_in_path_for(resource) 
    # session[:user_id] = resource.id 
    # end 



def current_user 
    @current_user ||= User.find(session[:user_id]) if session[:user_id] 
    end 

问题是,当我取消注释代码块以上,注册窗口工作色器件,但我得到以下错误,当我试图通过Facebook注册,我碰到下面的错误。

NoMethodError (undefined method `to_model' for 2:Fixnum): 
    app/controllers/sessions_controller.rb:9:in `facebook' 

我该如何让这两者合作?

回答

0

after_sign_in_path_for应该返回一个URL或资源

sign_in_and_redirect将使用URL /资源重定向页面

我们可以更新方法:

def after_sign_in_path_for(resource) 
    session[:user_id] = resource.id 
    root_path 
end 
+0

我只是通过删除解决的问题我明确定义的current_user方法。我想因为omniauth是在devise.rb中配置的,所以我不应该定义currenta_user。让我知道你的想法。 – user2441151

+0

是的,已经定义了'current_user'。我只是在使用'after_sign_in_path_for'的时候提出一些意见,以防你需要它:) –