Rails Koala - 获取一个未定义的方法`oauth_token =&#39;对于#<user:

时间:2017-03-06 00:26:13

标签: ruby-on-rails ruby facebook koala

=“”

为初学者问题道歉。我试图在我的应用程序上使用Koala gem。目前,我有设计和omniauth(Facebook登录)。而且他们工作得很好 - 我能够轻松注册用户。我现在想做的是利用Koala从我登录的用户访问Facebook数据。但是,我收到错误&#34;未定义的方法`oauth_token =&#39;为#

User.rb

class User < ApplicationRecord
  devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable, :omniauthable, omniauth_providers: [:facebook]

  def self.from_omniauth(auth)
    where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
      user.email = auth.info.email
      user.password = Devise.friendly_token[0,20]
      user.username = auth.info.name     
      user.avatar = URI.parse(auth.info.image)
      user.oauth_token = auth.credentials.token #Added this after learning about Koala
    end
  end

  def facebook
    @facebook ||= Koala::Facebook::API.new(oauth_token)
  end
end

Application_Controller.rb

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  before_action :configure_permitted_parameters, if: :devise_controller?
  before_filter :set_body_class
  before_filter :set_current_user

  def set_current_user
    User.current_user = current_user
  end

  def set_body_class
    @body_class = "#{controller_name} #{action_name}"
  end

  protected

  def after_sign_in_path_for(resource)
    # request.env['omniauth.origin'] || stored_location_for(resource) || root_path
    items_path
  end

  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up, keys: [:username, :name, :avatar])
    devise_parameter_sanitizer.permit(:account_update, keys: [:username,:name, :avatar])
  end
end

Schema.rb

create_table "users", force: :cascade do |t|
  t.string   "email",                  default: "", null: false
  t.string   "encrypted_password",     default: "", null: false
  t.string   "reset_password_token"
  t.datetime "reset_password_sent_at"
  t.datetime "remember_created_at"
  t.integer  "sign_in_count",          default: 0,  null: false
  t.datetime "current_sign_in_at"
  t.datetime "last_sign_in_at"
  t.string   "current_sign_in_ip"
  t.string   "last_sign_in_ip"
  t.datetime "created_at",                          null: false
  t.datetime "updated_at",                          null: false
  t.string   "avatar_file_name"
  t.string   "avatar_content_type"
  t.integer  "avatar_file_size"
  t.datetime "avatar_updated_at"
  t.string   "username"
  t.string   "provider"
  t.string   "uid"
  t.index ["email"], name: "index_users_on_email", unique: true
  t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end

迁移AddOmniauthToUsers

class AddOmniauthToUsers < ActiveRecord::Migration[5.0]
  def change
    add_column :users, :provider, :string
    add_column :users, :uid, :string
  end
end

enter image description here

1 个答案:

答案 0 :(得分:1)

尝试并致电self.outh_token。 outh_token属于一个用户,除非你将用户作为参数传递或使用“self”调用它自己,否则Rails不知道outh_token属于哪里。

  def facebook
    @facebook ||= Koala::Facebook::API.new(self.oauth_token)
  end

  def facebook(user)
    @facebook ||= Koala::Facebook::API.new(user.oauth_token)
  end