2012-11-03 72 views
1

我跟隨這名:tutorialRuby on Rails的授權是不保存

user = User.new :name => auth_hash["user_info"]["name"], :email => auth_hash["user_info"]["email"] 
user.authorizations.build :provider => auth_hash["provider"], :uid => auth_hash["uid"] 
user.save 
render :text => "Hi #{user.name}! You've signed up." 

我在瀏覽數據庫,並檢查表authorization和發現那位出表是空的。
這是爲什麼?

編輯: 我得到這個錯誤:

Oops, something went wrong: ["Authorizations is invalid"]

這是user.rb:

class User < ActiveRecord::Base 
    has_many :topics 
    has_many :authorizations 

    attr_accessible :email, :name 
    validates :name, :email, :presence => true 
end 

這是authorization.rb:

class Authorization < ActiveRecord::Base 

    belongs_to :user 
    validates :provider, :uid, :presence => true 

    attr_accessible :provider, :uid 
end 
+0

發佈您的用戶模型,並檢查'user.save'的返回值 - 如果它不是'true',那麼您可能有驗證錯誤。 – Thilo

回答

1

始終檢查返回值:

... 
if user.save 
    render :text => "Hi #{user.name}! You've signed up." 
else 
    # do something with the errors, e.g.: 
    render :text => "Oops, something went wrong: #{user.errors.full_messages}" 
end