小编sim*_*ons的帖子

在Rails中将多级嵌套表单标记为"脏"

我在Rails中有一个三级多嵌套表单.设置如下:项目有许多里程碑,里程碑有很多笔记.目标是使用JavaScript在页面中编辑所有内容,我们可以在页面中的项目中添加多个新的里程碑,并将新的Notes添加到新的和现有的里程碑.

一切都按预期工作,除了当我向现有的里程碑添加新笔记(新的里程碑在向他们添加笔记时工作正常)时,新笔记将不会保存,除非我编辑任何实际属于里程碑的字段以标记形式"脏"/编辑.

有没有办法标记里程碑,以便添加的新Notes将保存?

编辑:对不起,很难粘贴所有代码,因为有很多部分,但是这里有:

楷模

class Project < ActiveRecord::Base
  has_many :notes, :dependent => :destroy
  has_many :milestones, :dependent => :destroy

  accepts_nested_attributes_for :milestones, :allow_destroy => true
  accepts_nested_attributes_for :notes, :allow_destroy => true, :reject_if => proc { |attributes| attributes['content'].blank? }
end

class Milestone < ActiveRecord::Base
  belongs_to :project
  has_many :notes, :dependent => :destroy

  accepts_nested_attributes_for :notes, :allow_destroy => true, :allow_destroy => true, :reject_if => proc { |attributes| attributes['content'].blank? }
end

class Note < ActiveRecord::Base
  belongs_to :milestone
  belongs_to :project

  scope :newest, lambda { |*args| order('created_at …
Run Code Online (Sandbox Code Playgroud)

ruby forms ruby-on-rails nested-forms

5
推荐指数
1
解决办法
1610
查看次数

标签 统计

forms ×1

nested-forms ×1

ruby ×1

ruby-on-rails ×1