活动模型MassAssignmentSecurity错误(Active Model MassAssi

2019-08-01 18:20发布

我发现了一个错误:我正在开发与Facebook集成的web ..我在Ruby on Rails的新。 您可以与我的问题有所帮助。

ActiveModel::MassAssignmentSecurity::Error in SessionsController#create

Can't mass-assign protected attributes: name

这是我的控制器:

class SessionsController < ApplicationController
  def create
    #render :json => request.env['omniauth.auth'].to_yaml
   auth = request.env['omniauth.auth']
    unless @auth = Authorization.find_from_hash(auth)
      @auth = Authorization.create_from_hash(auth, current_user)
    end

    self.current_user = @auth.user  
    render :text => "Welcome, #{current_user.name}."
   end
end

这是我的用户模型:

class User < ActiveRecord::Base
  has_many :authorizations
  attr_accessor :name, :uid, :provider

  def self.create_from_hash!(hash)
    #create(:name => hash['user_info']['name'])
   create(:name => hash.info.name)
  end
end

这是我的授权模型:

class Authorization < ActiveRecord::Base
  belongs_to :user
  validates_presence_of :user_id, :uid, :provider
  validates_uniqueness_of :uid, :scope => :provider

  def self.find_from_hash(hash)
    find_by_provider_and_uid(hash['provider'], hash['uid'])
  end

  def self.create_from_hash(hash, user = nil)
    user ||= User.create_from_hash!(hash)
    Authorization.create(:user => user, :uid => hash['uid'], :provider => hash['provider'])
  end
end

我怎么能提前解决这个问题..谢谢:)

Answer 1:

你肯定已经启用了质量分配保护 (与config.active_record.whitelist_attributes = true在你的配置文件),所以你需要明确地说明哪些属性可以通过类似的方法来更新update_attributes 。 你做attr_accessible

User模式,替换下面的行(似乎没用)

attr_accessor :name, :uid, :provider

通过

attr_accessible :name, :uid, :provider

请参阅相关的问题attr_accessorattr_accessible进一步信息,请点击这里 , 这里和这里



文章来源: Active Model MassAssignmentSecurity Error