Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails SessionController中的NoMethodError#在用户模型中调用方法时创建_Ruby On Rails_Ruby_Ruby On Rails 3_Nomethoderror - Fatal编程技术网

Ruby on rails SessionController中的NoMethodError#在用户模型中调用方法时创建

Ruby on rails SessionController中的NoMethodError#在用户模型中调用方法时创建,ruby-on-rails,ruby,ruby-on-rails-3,nomethoderror,Ruby On Rails,Ruby,Ruby On Rails 3,Nomethoderror,我正在使用rails 3应用程序,并使用考拉gem连接到facebook graph api。我还使用omniauth来吸引用户 我得到以下错误: NoMethodError in SessionsController#create undefined method `add_friends' for #<User:0x007fcfcb1a87e8> app/models/user.rb:28:in `block in from_omniauth' app/models/user.

我正在使用rails 3应用程序,并使用考拉gem连接到facebook graph api。我还使用omniauth来吸引用户

我得到以下错误:

NoMethodError in SessionsController#create
undefined method `add_friends' for #<User:0x007fcfcb1a87e8>

app/models/user.rb:28:in `block in from_omniauth'
app/models/user.rb:6:in `tap'
app/models/user.rb:6:in `from_omniauth'
app/controllers/sessions_controller.rb:3:in `create'
会话控制器中的nomethoder错误#创建 未定义的方法“添加朋友”# app/models/user.rb:28:in'block in from_omniauth' app/models/user.rb:6:in'tap' app/models/user.rb:6:in'from_omniauth' app/controllers/sessions\u controller.rb:3:in'create' 在ruby文档中,它说:“当一个方法在一个没有定义它的接收者上被调用,并且由于缺少方法_而无法响应时,就会引发。”

但是我定义了一个metod,那么为什么会出现这个错误呢?我如何修复它?

在SessionController中,我执行以下操作:

class SessionsController < ApplicationController
  def create
    user = User.from_omniauth(env['omniauth.auth'])
    session[:user_id] = user.id
    redirect_to root_url, notice: "Signed in!"
  end
end
class sessioncontroller
这是我的用户模型:

class User < ActiveRecord::Base
  #attr_accessible :provider, :uid, :token, :email, :name, :first_name, :last_name, :image, :gender, :location, :school, :year
  has_many :friends

  def self.from_omniauth(auth)
    where(auth.slice(:provider, :uid)).first_or_initialize.tap do |user|
      user.provider = auth["provider"]
      user.uid = auth["uid"]
      user.name = auth["info"]["name"]
      user.first_name = auth["info"]["first_name"]
      user.last_name = auth["info"]["last_name"]
      user.image = auth["info"]["image"]
      user.email = auth["info"]["email"]
      user.gender = auth["extra"]["raw_info"]["gender"]
      user.location = auth["extra"]["raw_info"]["location"]["name"]          
      user.token = auth["credentials"]["token"]

      user.add_friends
      user.save!
      user

    end

    def add_friends
      @facebook.get_connections("me", "friends").each do |hash|
        self.friends.find(:name => hash['name'], :uid => hash['id']).first_or_create
      end
    end

    private 

    def facebook
      @facebook ||= Koala::Facebook::API.new(token)
    end
  end
end
class用户hash['name'],:uid=>hash['id'])。首先\u或\u创建
结束
结束
私有的
def facebook
@facebook | |=考拉::facebook::API.new(令牌)
结束
结束
结束

在您发布的代码中,
def add_friends
实际上在您的
from_omniauth
类方法中,因此最终也是类方法,而不是实例方法


把那个(和facebook的方法)移到一边,你应该会没事的。您可能还希望调用
facebook
方法,而不是直接访问实例变量

在您发布的代码中
def add_friends
实际上在您的
from_omniauth
类方法中,因此最终也是类方法,而不是实例方法


把那个(和facebook的方法)移到一边,你应该会没事的。您可能还希望调用
facebook
方法,而不是直接访问实例变量

你能给我看一下吗,我这样移动了这个方法:现在我得到了一个用于get_connections的方法:未定义的方法'get_connections'用于nil:nilclass你使用的是
@facebook
,没有设置它。Do
facebook.get_connections(…)
而不是I get:undefined local variable或方法'facebook'for#你还需要将facebook方法移出该类方法你能告诉我,我这样移动了方法:现在我得到了一个用于get_connections的方法,而不是:nil:nilclass的未定义方法“get_connections”。您正在使用
@facebook
,但没有设置它。Do
facebook.get_connections(…)
而不是I get:undefined local variable或'facebook'for#你还需要将facebook方法移出该类方法