Search code examples
ruby-on-railsrubyimagemodelomniauth

Method not detected in model " undefined method `process_uri' "


Im trying to figure out why i cant get my method to be identified. Maybe im not seeing things clearly and made a little mistake that someone can catch? Here is my code and when i click on the button i get this error.

User Model: when if auth.info.image.present? gets passed the process_uri gets called but my method isn't identified.

def self.from_omniauth(auth)

 anonymous_username = "NewUser#{User.last.id + 1}"
 generated_password = Devise.friendly_token[0,20]

  user = User.where(:email => auth.info.email, :username => anonymous_username).first


  if user
    return user
  else
    where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
      user.confirmed_at = Time.now
      user.fullname = auth.info.name
      user.provider = auth.provider
      user.uid = auth.uid
      user.username = anonymous_username
      user.email = auth.info.email
      user.password = generated_password

    end

if auth.info.image.present?
   avatar_url = process_uri(auth.info.image)
   user.update_attribute(:avatar, URI.parse(avatar_url))
end

  end
end


private 

  def process_uri(uri)
    require 'open-uri'
    require 'open_uri_redirections'
    open(uri, :allow_redirections => :safe) do |r|
      r.base_uri.to_s
    end
  end

As you can see I have a private method underneath def process_uri(uri) .. even when i take out private this still isn't being noticed... thank you!!


Solution

  • The problem here is that the private method process_uri is an instance method, whereas from_omniauth is a class method. In the context of a class method, the self object that the method would be called on is the class, not the instance, so you get an undefined method error because there is no class method process_uri. You can either define process_uri as a class method, or you can make it public and call it on the object itself (e.g. user.process_uri).