SessionsController#中的NoMethodError在用户模型中调用方法时创建

时间:2022-03-12 13:08:22

I am working on rails 3 app and using the koala gem to get a connection to the facebook graph api.I am also using omniauth to autenticate users.

我正在使用rails 3应用程序并使用koala gem来获取与facebook图表api的连接。我也使用omniauth来验证用户。

I get the following error:

我收到以下错误:

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'

In ruby docs it says: "Raised when a method is called on a receiver which doesn't have it defined and also fails to respond with method_missing."

在ruby docs中,它说:“当一个方法在接收器上调用时没有定义,也无法用method_missing响应。”

But I have a metod defined, so why do I get this error? And how can I fix it?

但我定义了一个metod,为什么我会收到此错误?我该如何解决?

In my SessionController I do something like this:

在我的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

This is my user model:

这是我的用户模型:

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

1 个解决方案

#1


1  

In the code you posted def add_friends is actually inside your from_omniauth class method and so ends up being a class method too, rather than an instance method.

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

Move that (and the facebook method) out of there and you should be ok. You probably also want to be calling your facebook method rather than accessing the instance variable directly.

将那个(以及facebook方法)从那里移开,你应该没问题。您可能还想调用您的facebook方法而不是直接访问实例变量。

#1


1  

In the code you posted def add_friends is actually inside your from_omniauth class method and so ends up being a class method too, rather than an instance method.

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

Move that (and the facebook method) out of there and you should be ok. You probably also want to be calling your facebook method rather than accessing the instance variable directly.

将那个(以及facebook方法)从那里移开,你应该没问题。您可能还想调用您的facebook方法而不是直接访问实例变量。