Rails - 如何在不使用accepts_nested_attributes_for的情况下管理嵌套属性?

2024-04-20

我的问题是我遇到了accepts_nested_attributes_for的限制,所以我需要弄清楚如何自己复制该功能以获得更大的灵活性。 (请参阅下文,了解到底是什么让我困惑。)所以我的问题是:如果我想模仿和增强accepts_nested_attributes_for,我的表单、控制器和模型应该是什么样子?真正的技巧是我需要能够使用现有关联/属性更新现有模型和新模型。

我正在构建一个使用嵌套表单的应用程序。我最初使用这个 RailsCast 作为蓝图(利用accepts_nested_attributes_for):Railscast 196:嵌套模型形式 http://railscasts.com/episodes/196-nested-model-form-revised.

我的应用程序是包含作业(任务)的清单,我让用户更新清单(名称、描述)并以单一表单添加/删除关联的作业。这很有效,但是当我将其合并到我的应用程序的另一个方面:通过版本控制的历史记录时,我遇到了问题。

我的应用程序的一个重要部分是我需要记录我的模型和关联的历史信息。我最终推出了自己的版本控制(here https://stackoverflow.com/questions/15235524/rails-3-2-app-should-i-use-a-versioning-gem-paper-trail-or-vestal-versions-o是我的问题,我在其中描述了我的决策过程/考虑因素),其中很大一部分是一个工作流程,我需要创建旧事物的新版本,对新版本进行更新,存档旧版本。这对于用户来说是不可见的,他们将体验视为简单地通过 UI 更新模型。

代码 - 型号

#checklist.rb
class Checklist < ActiveRecord::Base
  has_many :jobs, :through => :checklists_jobs
  accepts_nested_attributes_for :jobs, :reject_if => lambda { |a| a[:name].blank? }, :allow_destroy => true
end

#job.rb
class Job < ActiveRecord::Base
  has_many :checklists, :through => :checklists_jobs
end

代码 - 当前形式(注意:@jobs 被定义为清单控制器编辑操作中此清单的未归档作业;@checklist 也是如此)

<%= simple_form_for @checklist, :html => { :class => 'form-inline' } do |f| %>
  <fieldset>
    <legend><%= controller.action_name.capitalize %> Checklist</legend><br>

    <%= f.input :name, :input_html => { :rows => 1 }, :placeholder => 'Name the Checklist...', :class => 'autoresizer'  %>
    <%= f.input :description, :input_html => { :rows => 3 }, :placeholder => 'Optional description...', :class => 'autoresizer' %>

    <legend>Jobs on this Checklist - [Name] [Description]</legend>

    <%= f.fields_for :jobs, @jobs, :html => { :class => 'form-inline' } do |j| %>
        <%= render "job_fields_disabled", :j => j %>
    <% end %>
    </br>
    <p><%= link_to_add_fields "+", f, :jobs %></p>

    <div class="form-actions">
      <%= f.submit nil, :class => 'btn btn-primary' %>
      <%= link_to 'Cancel', checklists_path, :class => 'btn' %>
    </div>
  </fieldset>
<% end %>

代码 - checklists_controller.rb#Update 的片段

def update
  @oldChecklist = Checklist.find(params[:id])

# Do some checks to determine if we need to do the new copy/archive stuff
  @newChecklist = @oldChecklist.dup
  @newChecklist.parent_id = (@oldChecklist.parent_id == 0) ? @oldChecklist.id : @oldChecklist.parent_id
  @newChecklist.predecessor_id = @oldChecklist.id
  @newChecklist.version = (@oldChecklist.version + 1)
  @newChecklist.save

# Now I've got a new checklist that looks like the old one (with some updated versioning info).

# For the jobs associated with the old checklist, do some similar archiving and creating new versions IN THE JOIN TABLE
  @oldChecklist.checklists_jobs.archived_state(:false).each do |u|
    x = u.dup
    x.checklist_id = @newChecklist.id
    x.save
    u.archive
    u.save
  end

# Now the new checklist's join table entries look like the old checklist's entries did
# BEFORE the form was submitted; but I want to update the NEW Checklist so it reflects 
# the updates made in the form that was submitted.
# Part of the params[:checklist] has is "jobs_attributes", which is handled by
# accepts_nested_attributes_for. The problem is I can't really manipulate that hash very
# well, and I can't do a direct update with those attributes on my NEW model (as I'm 
# trying in the next line) due to a built-in limitation.
  @newChecklist.update_attributes(params[:checklist])

这就是我遇到的 Accepts_nested_attributes_for 限制的地方(它有很好的记录here https://github.com/rails/rails/issues/7256。我收到“无法为 ID=Y 的 Model2 找到 ID=X 的 Model1”异常,这基本上是按设计的。

那么,我如何创建多个嵌套模型并在父模型的表单上添加/删除它们,类似于accepts_nested_attributes_for,但由我自己完成?

我见过的选项 - 这是最好的选项之一吗?真正的技巧是我需要能够使用现有关联/属性更新现有模型和新模型。我无法链接它们,所以我只是命名它们。

繁文缛节(在 github 上) Virtus(也是 github)

感谢您的帮助!


您可能想要删除复杂的 Accept_nested 内容并创建一个自定义类或模块来包含所有所需的步骤。

这篇文章里有一些有用的东西

http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/ http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/

特别是第3点

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Rails - 如何在不使用accepts_nested_attributes_for的情况下管理嵌套属性? 的相关文章

随机推荐