ruby-on-rails - 嵌套形式/茧 : children not saving

标签 ruby-on-rails ruby-on-rails-4 nested-forms cocoon-gem

在我的应用程序中,我有联系人,其中有许多目标(目标属于联系人)。我使用嵌套表单来创建/更新两个模型的数据,但是在创建/更新操作期间仅保存第一个子目标,这可以从下面传递的参数中看出:

Started PATCH "/contacts/2" for 127.0.0.1 at 2014-05-11 19:10:40 -0400
Processing by ContactsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"wKgOPegIJOFyrrLAnbdD67qqATFXrPNqMIT7I/tpNfo=", "contact"=>{"name"=>"Steven Tyler", "title"=>"Manager", "company"=>"Dell", "email"=>"<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8dfef9e8fbe8cde9e8e1e1a3eee2e0" rel="noreferrer noopener nofollow">[email protected]</a>", "notes"=>"cool guy", "goals_attributes"=>{"0"=>{"title"=>"goal1", "due_date(1i)"=>"2014", "due_date(2i)"=>"5", "due_date(3i)"=>"11", "notes"=>"fdfsd", "_destroy"=>"false", "id"=>"13"}}}, "commit"=>"submit", "id"=>"2"}
  Contact Load (0.1ms)  SELECT  "contacts".* FROM "contacts"  WHERE "contacts"."id" = ? LIMIT 1  [["id", 2]]
   (0.1ms)  begin transaction
  Goal Load (0.3ms)  SELECT "goals".* FROM "goals"  WHERE "goals"."contact_id" = ? AND "goals"."id" IN (13)  [["contact_id", 2]]
   (0.1ms)  commit transaction
Redirected to http://localhost:3000/contacts/2

这是我的模型:

class Contact < ActiveRecord::Base

  belongs_to :user
  has_many :goals, dependent: :destroy
  accepts_nested_attributes_for :goals, allow_destroy: true, reject_if: lambda {|attributes| attributes['title'].blank? && attributes['notes'].blank?}
  validates_presence_of :user_id,:name,:title,:email
end

class Goal < ActiveRecord::Base

  belongs_to :contact
  validates_presence_of :title, :due_date
end

并且,联系人 Controller 中的强大参数:

def contact_params
      params.require(:contact).permit(:name, :title, :company, :email, :notes, goals_attributes: [:title, :due_date, :notes, :contact_id, :_destroy, :id])
    end

以下是使用的两种形式:https://gist.github.com/nowgeez/5fbe99e814330c1b371c 知道为什么只保存第一个目标,而不保存表单中创建的任何其他后续目标吗?先谢谢您的帮助。

最佳答案

如文档中所述,使用正确的 div 嵌套非常重要。

所以在你的情况下你应该写:

<%= simple_form_for(@contact) do |f| %>

  <%= f.input :name %>
  <%= f.input :title %>
  <%= f.input :company %>
  <%= f.input :email %>
  <%= f.input :notes %>

  <h3>Goals:</h3>
  <div id="goals">
    <%= f.simple_fields_for(:goals) do |goal| %>
      <%= render 'goal_fields', :f => goal %>
    <% end %>  
    <div class="links">          
      <%= link_to_add_association 'add goal', f, :goals %>
    </div>
  </div>

  <%= f.submit :submit %>

<% end %>

为了清楚起见,我删除了错误 block 。 目标本身应该包含在 div 中,以便删除工作。

<div class="nested-fields>
  <%= f.input :title %>
  <%= f.input :due_date %>
  <%= f.input :notes %>

  <%= link_to_remove_association "remove goal", f %>
</div>

旁注:我讨厌写 erb,它更加冗长,从你的要点来看,它似乎正确嵌套,但实际上并非如此。哈姆尔(Haml)恕我直言更清楚。 只是为了证明我的观点,与上面的 haml 形式相同:

= simple_form_for @contact do |f|
  = f.input :name
  = f.input :title
  = f.input :company
  = f.input :email
  = f.input :notes

  %h3 Goals
  #goals
    = f.simple_fields_for :goals do |goal|
      = render 'goal_fields', :f => goal
    .links
      = link_to_add_association 'add goal', f, :goals
  = f.submit

但我知道这是一个品味问题。

关于ruby-on-rails - 嵌套形式/茧 : children not saving,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23598982/

相关文章:

ruby-on-rails - rails : How do I display flash[:notice] modally

ruby-on-rails - 如何将 CanCanCan 与枚举字段一起使用?

ruby-on-rails - ruby rails : what does build_model do?

ruby-on-rails - Rails 4.2 保存 has_and_belongs_to_many 关联 ID

ruby-on-rails - 在 rails_admin 中为 has_many :through relationship in rails 4 使用 orderable

ruby-on-rails - 如何使用 Ruby on Rails 进行具有二值范围和作用域的查询?

ruby-on-rails - Rails has_many :through and N+1

ruby-on-rails - 如何使用自定义谓词通过干式验证来验证多个字段?

ruby-on-rails - Rails 根据 Rails 环境定义 root url

ruby-on-rails - 没有路由匹配错误 Rails 4