2014-09-05 5 views
0

Я получаю следующую ошибку при попытке входа в систему через Google + с помощью gogle 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: google_oauth2 в моем файле devise.rb.

routes.rb

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

ответ

1

Вы звоните find_for_google_oauth2 из omniauth_callbacks_controller, но вы используете неправильное имя метода google_oauth2. Вы должны заменить google_oauth2 на find_for_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

Смежные вопросы