Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails:在保存更新之前操作参数-错误的方法?_Ruby On Rails - Fatal编程技术网

Ruby on rails Rails:在保存更新之前操作参数-错误的方法?

Ruby on rails Rails:在保存更新之前操作参数-错误的方法?,ruby-on-rails,Ruby On Rails,首先,我觉得我用了错误的方法,但我不知道该怎么做。这也有点难以解释,所以请耐心听我说 我使用Javascript允许用户在编辑表单中添加多个文本区域,但这些文本区域用于单独的模型。它基本上允许用户在两个模型而不是一个模型中编辑信息。以下是关系: class Incident < ActiveRecord::Base has_many :incident_notes belongs_to :user end class IncidentNote < ActiveRecor

首先,我觉得我用了错误的方法,但我不知道该怎么做。这也有点难以解释,所以请耐心听我说

我使用Javascript允许用户在编辑表单中添加多个文本区域,但这些文本区域用于单独的模型。它基本上允许用户在两个模型而不是一个模型中编辑信息。以下是关系:

class Incident < ActiveRecord::Base
  has_many   :incident_notes
  belongs_to :user
end

class IncidentNote < ActiveRecord::Base
  belongs_to :incident
  belongs_to :user
end

class User < ActiveRecord::Base
  has_many :incidents
  has_many :incident_notes
end
我想我可以添加如下内容:

params[:incident].incident_note_attributes.each do |inote_atts|
  for att in inote_atts
    if att.id == nil
      att.user_id = current_user.id
    end
  end
end
但显然,
事件\u注意\u属性
不是一种方法。所以我不知道该怎么办。我怎样才能解决这个问题


对不起,这是文字墙。非常感谢您的帮助

我有一个类似的要求,我就是这样解决的:

class Incident < ActiveRecord::Base
  has_many   :incident_notes
  belongs_to :user

  attr_accessor :new_incident_note

  before_save :append_incident_note

  protected

  def append_incident_note
    self.incident_notes.build(:note => self.new_incident_note) if !self.new_incident_note.blank?
  end
end
类事件self.new\u incident\u note)如果!self.new\u incident\u note.blank?
结束
结束
然后在表单中,您只需为使用标准的rails
form\u,并使用
new\u事件注释
作为属性

我之所以选择这种方法,是因为我知道它只是通过少量的数据验证将数据放入注释中。如果您有深入的验证,那么我建议对
使用
accepts\u nested\u attributes\u和对
使用
fields\u。这是非常有记录的

class Incident < ActiveRecord::Base
  has_many   :incident_notes
  belongs_to :user

  attr_accessor :new_incident_note

  before_save :append_incident_note

  protected

  def append_incident_note
    self.incident_notes.build(:note => self.new_incident_note) if !self.new_incident_note.blank?
  end
end