Question

Facebook login works on my localhost but when I push the code to my server and try to login I get this error

Completed 500 Internal Server Error in 5ms

ActiveRecord::UnknownAttributeError (unknown attribute: name):
app/models/confirmable.rb:20:in `initialize'
app/models/user.rb:83:in `find_for_facebook_oauth'
app/controllers/omniauth_callbacks_controller.rb:5:in `facebook'

user.rb

 def self.find_for_facebook_oauth(auth, signed_in_resource=nil)
  user = User.where(:provider => auth.provider, :uid => auth.uid).first
  if user
   return user
  else
   registered_user = User.where(:email => auth.info.email).first
   if registered_user
    return registered_user
   else
    user = User.create(name:auth.extra.raw_info.name,
                          provider:auth.provider,
                          uid:auth.uid,
                          email:auth.info.email,
                          password:Devise.friendly_token[0,20],
                        )
   end

  end

 end

omniauth_callbacks_controller.rb

def facebook     
 @user = User.find_for_facebook_oauth(request.env["omniauth.auth"], current_user)      
 if @user.persisted?       
  sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated
  set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
 else
  session["devise.facebook_data"] = request.env["omniauth.auth"]
  redirect_to new_user_registration_url
 end
end
Était-ce utile?

La solution

Looking at the error, it seems like there is no name attribute in users table. While creating a user record you are passing the non-existing attribute and getting the error.

To resolve this issue,

Either add a column to users table called name By:

Generating a migration
  rails generate migration AddNameToUsers name:string
Running 
  rake db:migrate

Or

Remove the name attribute and value from list of arguments passed to User.create method.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top