活动模型MassAssignmentSecurity错误

活动模型MassAssignmentSecurity错误

本文介绍了活动模型MassAssignmentSecurity错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个错误:我正在使用Facebook Integration开发一个网站.能为我解决问题吗?

I'm Getting an Error: I'm developing a web with Facebook Integration.. I'm new in ruby on rails. can you help with my problem.

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

我该如何解决..在此先感谢:)

how can I fix this .. Thanks in advance :)

推荐答案

您当然已启用质量分配保护(在配置文件中带有config.active_record.whitelist_attributes = true),因此您需要明确指出哪些属性可以通过update_attributes之类的方法进行更新.您可以使用attr_accessible来实现.

You certainly have enabled mass assignment protection (with config.active_record.whitelist_attributes = true in your config file), so you need to explicitly indicate which attributes can be updated by methods like update_attributes. You do it with attr_accessible.

User模型中,替换以下行(这似乎没用)

In the User model, replace the following line (that seems useless)

attr_accessor :name, :uid, :provider

通过

attr_accessible :name, :uid, :provider

此处中查看与attr_accessorattr_accessible有关的问题 a>,此处

See questions related to attr_accessor and attr_accessible for further informations here, here or here

这篇关于活动模型MassAssignmentSecurity错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 06:09