1

我已将以下 setter/getter 方法添加到我的模型中,但每当我尝试保存表单时,我都会收到有关质量分配的错误。据我了解,这应该是如何工作的,如果找不到对手名称,它将向数据库添加一个条目

def opponent_name
opponent.try(:name)
end

def opponent_name(name)
    self.opponent = Opponent.find_or_create_by_name(name) if name.present?
  end

这是来自控制台日志的错误

    Started POST "/events" for 127.0.0.1 at 2013-03-26 19:07:26 +1100
Processing by EventsController#create as JS
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"h7OrLKeDL/9KmZeGZeO+QTWHtlUdOlaMqnoMGhYaDUU=", "event"=>{"datetime(3i)"=>"2", "datetime(2i)"=>"3", "datetime(1i)"=>"2013", "datetime(4i)"=>"00", "datetime(5i)"=>"00", "event"=>"1", "location_id"=>"7", "duration"=>"30", "arrival_time"=>"30", "opponent_name"=>"Test", "home_or_away"=>"Home"}, "commit"=>"Create Event"}
  User Load (0.9ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 4 LIMIT 1
Completed 500 Internal Server Error in 14ms

ActiveModel::MassAssignmentSecurity::Error (Can't mass-assign protected attributes: opponent_name):
  app/controllers/application_controller.rb:22:in `catch_not_found'

对手模型

class Opponent < ActiveRecord::Base
  has_many  :events
  belongs_to  :team

  attr_accessible :name, :team_id

  validates :name, :presence => true
end

事件模型

class Event < ActiveRecord::Base
  include PublicActivity::Model
  tracked
  belongs_to :location
  belongs_to :opponent
  belongs_to :team
  belongs_to :result
  has_many :availabilities, :dependent => :destroy

  def opponent_name
    opponent.try(:name)
  end

  def opponent_name(name)
    self.opponent = Opponent.find_or_create_by_name(name) if name.present?
  end

  attr_accessible :location_id, :user_id, :datetime, :score_for, :score_against, :event,
                  :team_id, :home_or_away, :arrival_time, :duration, :selected_players, :date, :time, :result_id

  validates :event, :location_id, :team_id, :presence => true
  validates_numericality_of :team_id, :only_integer => true, :greater_than_or_equal_to =>0, :message => " needs to be set, please contact your Administrator"
  #validates_numericality_of :arrival_time, :only_integer =>true, :greater_than_or_equal_to =>0, :message => " must be greater than 0 minutes", :allow_blank => true
  validates :home_or_away, :presence => true, :if => :event == 1
  validates :score_for, :presence => true, :if => :score_against
  validates :score_against, :presence => true, :if => :score_for

  EVENT_TYPES = [['Game', 1], ['Training', 2], ['Social Event', 3]]
  HOME_OR_AWAY = [:Home, :Away]

end
4

4 回答 4

2

看看ActiveModel::MassAssignmentSecurity::ClassMethods

我相信你必须在你的对手模型中添加以下语句

attr_accessible :opponent_name
于 2013-03-26T08:16:09.543 回答
1

尝试放入您的事件模型

attr_accessible :opponent_name

然后它应该清除错误

编辑:

只是更新一个答案,但所有学分都归于 Mischa进行此编辑。

问题可能是你定义了你的二传手 def

于 2013-03-26T10:42:00.463 回答
0

参考@Mischa

问题可能是你定义了你的二传手,比如def对手名称(名称),而它应该是def对手名称=(名称)。当您这样做并 attr_accessible :opponent_name 时,它​​可能会起作用。不知道为什么它在那条线上出错。似乎与错误无关。

于 2013-03-26T22:49:56.390 回答
0

如果对手名称是模型数据库表中的一个字段,那么 Rails 已经为该属性定义了 getter 和 setter。您需要做的就是添加 attr_accessible :opponent_name

于 2013-03-26T11:26:57.447 回答