SchedulesController #creore中的ActiveModel :: MassAssignmentSecurity :: Error

时间:2012-11-03 09:12:28

标签: ruby-on-rails activerecord attr-accessor

我试图声明一些虚拟属性将用于将日期和时间组合在一起给我日期时间,但我不断收到以下错误。正如您可以从下面的代码示例中看到的,如何设置属性以及我传递给他们的信息。

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"}

控制器

    # 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

模型

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

3 个答案:

答案 0 :(得分:1)

我相信您收到MassAssignmentSecurity错误,因为您在实例化新计划时将虚拟属性timedate传递到Schedule.new,因为它们不在列表中列入白名单的属性,Rails假定它们是不应该被批量分配的属性。您应该在启动新计划之前从params删除它们,然后(如果需要)直接设置相应的实例变量:

def create
  date = params[:schedule].delete(:date)
  time = params[:schedule].delete(:time)
  @schedule = Schedule.new(params[:schedule])
  @schedule.date = date
  @schedule.time = time
  ...

如果您希望在保存模型时从datetimedate设置time,则应创建一个before_save回调来执行此操作(可能您已经拥有)。

答案 1 :(得分:0)

检查无法进行的参数。然后这样做:

date = params[:schedule]
@schedule = Schedule.new(params[:schedule]) do |schedule|
  schedule.date = date
  ...
end

或者,您可以将质量分配规则设置为非严格:

config.active_record.mass_assignment_sanitizer = :logger

并且不会出现这些异常,只会记录过滤后的属性。然后你可以这样做:

@schedule = Schedule.new(params[:schedule]) do |schedule|
  schedule = params[:schedule][:date]
  ...
end

答案 2 :(得分:0)

为自己想出了这个,只是在最后添加了数据库中的字段