我遇到了一个错误:我正在使用 Facebook 集成开发一个网络。我是 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
我该如何解决这个问题..提前谢谢:)