2012-11-03 75 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