Rails Facebook登录适用于本地环境但不适用于生产

时间:2014-03-25 02:34:17

标签: ruby-on-rails facebook devise omniauth

Facebook登录在我的本地主机上工作,但当我将代码推送到我的服务器并尝试登录时,我收到此错误

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

1 个答案:

答案 0 :(得分:0)

查看错误,似乎name表中没有users属性。 在创建用户记录时,您将传递不存在的属性并获取错误。

要解决此问题,

将一列添加到名为name的用户表中:

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

或者

从传递给name方法的参数列表中删除User.create属性和值。