问题描述
我遇到了一个错误:我正在使用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_accessor
和attr_accessible
有关的问题 a>,此处或
See questions related to attr_accessor
and attr_accessible
for further informations here, here or here
这篇关于活动模型MassAssignmentSecurity错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!