2014-09-05 190 views
0

尝试通过google +使用google_oauth2宝石登录时出现以下错误。Google+注册Devise and Rails(google_oauth2)

undefined method `find_for_google_oauth2' for #<Class:0x007ff70a337148> 

以下是我为注册而更改的三个文件。

user.rb

def google_oauth2 
    user = User.from_omniauth(request.env["omniauth.auth"]) 
    if user.persisted? 
    flash.notice = "Signed in Through Google!" 
    sign_in_and_redirect user 
    else 
    session["devise.user_attributes"] = user.attributes 
    flash.notice = "You are almost Done! Please provide a password to finish setting up your account" 
    redirect_to new_user_registration_url 
    end 
end 

omniauth_callbacks_controller.rb

def google_oauth2 
    # You need to implement the method below in your model (e.g. app/models/user.rb) 
    @user = User.find_for_google_oauth2(request.env["omniauth.auth"], current_user) 

    if @user.persisted? 
    flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Google" 
    sign_in_and_redirect @user, :event => :authentication 
    else 
    session["devise.google_data"] = request.env["omniauth.auth"] 
    redirect_to new_user_registration_url 
    end 
end 

,我已经添加config.omniauth:在我devise.rb文件google_oauth2。

的routes.rb

devise_for :users, :controllers => { :registrations => "registrations", :sessions => "sessions", :omniauth_callbacks => "users/omniauth_callbacks" } 

回答

1

您从omniauth_callbacks_controller调用find_for_google_oauth2,但您使用了错误的方法名google_oauth2。您应该用find_for_google_oauth2替换google_oauth2

而且好像user.rb中的代码不正确,因为它包含控制器代码。你看到它看起来像你的控制器代码一样吗? :)

为user.rb正确的代码

def self.find_for_google_oauth2(access_token, signed_in_resource=nil) 
    data = access_token.info 
    user = User.where(:email => data["email"]).first 

    # Uncomment the section below if you want users to be created if they don't exist 
    # unless user 
    #  user = User.create(name: data["name"], 
    #  email: data["email"], 
    #  password: Devise.friendly_token[0,20] 
    # ) 
    # end 
    user 
end 

在这里阅读更多:https://github.com/zquestz/omniauth-google-oauth2#devise

相关问题