【问题标题】:Mass Assignment confusion in rails association铁路协会中的质量分配混乱
【发布时间】:2013-06-24 00:04:05
【问题描述】:

遇到一些我确定是基本问题的问题,但也很难在 SO 上找到一个好的答案。

我有一个用户表和一个授权表,这是我的模型:

##Authorization.rb
class Authorization < ActiveRecord::Base
  attr_accessible :provider, :uid, :user_id
  belongs_to :user
  validates :provider, :uid, :presence => true


  def self.find(auth_hash)
    find_by_provider_and_uid(auth_hash["provider"],
                             auth_hash["uid"])
  end

  def self.create_with_hash(auth_hash)
    #if they've already registered, then just return that authorization

    unless auth = find_by_provider_and_uid(auth_hash["provider"],
                                           auth_hash["uid"])
      user = User.create(name: auth_hash["info"]["name"],
                         email: auth_hash["info"]["email"],
                         nickname: nil,
                         firstname: auth_hash["info"]["first_name"],
                         location: auth_hash["user_location"]
                         )

      auth = create(user: user,
                    provider: auth_hash["provider"],
                    uid: auth_hash["uid"])
    end

    auth
  end

end

还有我的用户模型:

##User.rb
require 'bcrypt'

class User < ActiveRecord::Base
  include BCrypt

  #accessible and settable properties
  attr_accessible  :name, :email, :nickname, :firstname, :location

  #relations
  has_many :authorizations, dependent: :destroy

  #validations
  validates :name, :email, :firstname,  :presence => true
  validates :email, :uniqueness => true
  validates :nickname, :uniqueness => true

  #always make sure their email and nickname are lowercased
  def before_validation(user)
    user.email.downcase!
    user.email = Password.create(email)
    user.nickname.downcase!
  end

  def after_save(user)
    user.email = Password.new(user.email)
  end

  def is_nickname_available?(nickname)
    Users.find_by_nickname(nickname.downcase).blank?
  end

  def add_nickname(user_id, nickname)
    #todo: error handling
    user = Users.find(user_id).update_attribute(nickname: nickname)
  end

  def add_provider(auth_hash)
    #Check if the provider already exists, so we don't add it twice
    unless
      authorizations.find_by_provider_and_uid(auth_hash["provider"],
                                              auth_hash["uid"])
      Authorization.create    user_id:self.id,
        provider: auth_hash["provider"],
        uid: auth_hash["uid"]
    end
  end

end

在控制器中,如果我尝试这样做:Authorization.create_with_hash(auth_hash),我会收到错误:

Can't mass-assign protected attributes: user

在我的 Authorization.rb 模型中的 auth = create(user: user, provider: auth_hash["provider"], uid: auth_hash["uid"]) 线上。

所以,我对此很陌生,但不清楚为什么这不起作用。有人可以解释我做错了什么吗?

谢谢 穆斯塔法

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3.2 mass-assignment


    【解决方案1】:

    使用attr_accessible :provider, :uid, :user 应该可以。或者改变

    auth = create(user: user,
                  provider: auth_hash["provider"],
                  uid: auth_hash["uid"])
    

    auth = create(user_id: user.id,
                  provider: auth_hash["provider"],
                  uid: auth_hash["uid"])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-08
      • 1970-01-01
      • 1970-01-01
      • 2012-08-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多