2

エラーが発生しました: 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

どうすればこれを修正できますか..事前に感謝します:)

4

1 に答える 1

11

確かに(構成ファイルで)一括割り当て保護config.active_record.whitelist_attributes = trueを有効にしているため、 などの方法で更新できる属性を明示的に示す必要がありますupdate_attributes。で行いattr_accessibleます。

モデルで、User次の行を置き換えます (役に立たないようです)。

attr_accessor :name, :uid, :provider

attr_accessible :name, :uid, :provider

詳細については、ここここ、またはここに関連する質問を参照してくださいattr_accessorattr_accessible

于 2012-10-22T10:20:34.527 に答える