[英]ActiveModel::MassAssignmentSecurity::Error in SchedulesController#create
im trying to declare some virtual attributes that will be used to combine date and time together given me date time, though i keep getting the following error. 我试图声明一些虚拟属性,这些属性将给我日期时间,以便将日期和时间结合在一起,尽管我不断收到以下错误。 as you can see from the code examples below, how im setting the attributes, and what information im passing through to them.
从下面的代码示例中可以看到,如何设置属性以及传递给它们的信息是什么。
ActiveModel::MassAssignmentSecurity::Error in SchedulesController#create
{"utf8"=>"✓",
"authenticity_token"=>"j4V6DAqK5U/+ZGaSKDlrEoOBqXu3pEq/FM51ingi2sg=",
"schedule"=>{"event"=>"1",
"result_id"=>"",
"date"=>"04/11/2012",
"arrival_time"=>"08:00 PM",
"time"=>"08:00 PM",
"duration"=>"1800",
"location_id"=>"11",
"selected_players"=>["",
"41",
"38"],
"team_id"=>"1",
"opponent_id"=>"1",
"home_or_away"=>"Home"},
"commit"=>"Save Event"}
controller 控制者
# POST /schedules
# POST /schedules.json
def create
params[:schedule].delete(:date)
params[:schedule].delete(:time)
@user = User.find(current_user)
@players = User.where(:team_id => current_user[:team_id]).all
params[:schedule][:selected_players].compact
#params[:schedule][:selected_players].reject!{|selected_players| selected_players==""}
#@availabilities = @user.availabilities.create(:unique_id => params[:schedule][:id])
@schedule = Schedule.new(params[:schedule])
respond_to do |format|
if @schedule.save
#Notifier.event_added(@user,@schedule).deliver
format.html { redirect_to(schedules_url,
:notice => "#{event_display_c(@schedule.event)} vs #{@schedule.opponent.name} was successfully created.") }
format.json { render :json => @schedule, :status => :created, :location => @schedule }
else
format.html { render :action => "new" }
format.json { render :json => @schedule.errors, :status => :unprocessable_entity }
end
end
end
model 模型
class Schedule < ActiveRecord::Base
belongs_to :location
belongs_to :opponent
belongs_to :team
belongs_to :result
has_many :availabilities, :dependent => :destroy
attr_accessible :location_id, :user_id, :opponent_id, :datetime, :score_for, :score_against, :event,
:team_id, :home_or_away, :arrival_time, :duration, :selected_players, :result_id
attr_accessor :date, :time
I believe you are getting a MassAssignmentSecurity error because you are passing the virtual attributes time
and date
into Schedule.new
when you instantiate the new schedule, and since they are not in the list of whitelisted attributes, Rails assumes they are attributes that should not be mass assigned. 我相信您会收到MassAssignmentSecurity错误,因为您在实例化新计划时将虚拟属性
time
和date
传递到Schedule.new
,并且由于它们不在白名单属性列表中,因此Rails假定它们是不应被使用的属性。质量分配。 You should delete them from params
before initiating the new schedule, and then (if needed) set the corresponding instance variables directly: 您应该在启动新计划之前从
params
删除它们,然后(如果需要)直接设置相应的实例变量:
def create
date = params[:schedule].delete(:date)
time = params[:schedule].delete(:time)
@schedule = Schedule.new(params[:schedule])
@schedule.date = date
@schedule.time = time
...
If you want to then set datetime
from date
and time
when the model is saved, you should create an before_save
callback to do that (maybe you already have). 如果要从保存模型的
date
和time
开始设置datetime
,则应创建一个before_save
回调函数来执行此操作(也许已经有了)。
Check the parameters which are not being made acessible. 检查不需要的参数。 Then do this:
然后执行以下操作:
date = params[:schedule]
@schedule = Schedule.new(params[:schedule]) do |schedule|
schedule.date = date
...
end
alternatively, you can set the mass-assignment rules to non-strict: 或者,您可以将质量分配规则设置为非严格:
config.active_record.mass_assignment_sanitizer = :logger
and instead of having these exceptions, the filtered attributes will just be logged. 而不是具有这些异常,而是将记录已过滤的属性。 then you can do this:
那么您可以执行以下操作:
@schedule = Schedule.new(params[:schedule]) do |schedule|
schedule = params[:schedule][:date]
...
end
自己弄清楚了,最后才在数据库中添加了字段
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.