【问题标题】:Ruby on Rails Authorization isn't savedRuby on Rails 授权未保存
【发布时间】:2012-10-23 23:57:12
【问题描述】:

我正在关注这个:tutorial

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 并发现表是空的。
这是为什么呢?

编辑: 我收到此错误:

糟糕,出了点问题:[“授权无效”]

这是user.rb:

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

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

这是授权.rb:

class Authorization < ActiveRecord::Base

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

  attr_accessible :provider, :uid
end

【问题讨论】:

  • 发布你的用户模型,并检查user.save的返回值——如果不是true,你可能有一个验证错误。

标签: ruby-on-rails ruby authentication authorization omniauth


【解决方案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

【讨论】:

    猜你喜欢
    • 2013-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-31
    • 1970-01-01
    相关资源
    最近更新 更多