Rails:使用嵌套表单同时保存父级和子级

时间:2017-04-06 15:50:42

标签: ruby-on-rails ruby erb

我有一个Event模型和一个Packages模型:

# event.rb
belongs_to :organization, optional: true
belongs_to :event_type, optional: true

has_many :packages

has_many :collaborations
has_many :messages, through: :collaborations
has_many :forms, through: :collaborations

# package.rb
belongs_to :event

has_many :package_collaborations
has_many :collaborations, through: :package_collaborations

我希望用户能够使用嵌套表单同时创建一个Event和Packages:

[IMAGE]

这是我的两种形式:

#_event_form.rb (simplified for easier reading)
<%= form_for(event) do |f| %>

  <%= render 'shared/error_messages', object: f.object %>
  <br>
  <div class="form-group">
    <%= f.label :title %>
    <%= f.text_field :title, class: 'form-control' %>
  </div>

<% if f.object.new_record? %>
  <%= f.hidden_field :status, value: 'upcoming' %>
<% else %>
  <div class="form-group">
    <%= f.label :attendee_count %>
    <%= f.number_field :attendee_count, class: 'form-control' %>
  </div>
  <div class="form-group">
    <%= f.label :status %>
    <%= f.select :status, [['Upcoming','upcoming'],['Completed','completed']], {}, class: 'form-control' %>
  </div>
<% end %>

  <hr>

  <div class="form-group required">
    <%= f.label :started_at, 'Starts' %>
    <div class='input-group date form_datetime'>
      <% if event.started_at.present? %>
        <%= f.text_field :started_at, class: 'form-control', value: event.started_at.strftime("%m/%d/%Y %H:%M %p") %>
      <% end %>
    </div>
  </div>

  <div class="form-group required">
    <%= f.label :ended_at, 'Ends' %>
    <div class='input-group date form_datetime'>
      <% if event.ended_at.present? %>
        <%= f.text_field :ended_at, class: 'form-control', value: event.ended_at.strftime("%m/%d/%Y %H:%M %p") %>
      <% end %>
    </span>
    </div>
  </div>

  <%= f.hidden_field :platform_type, value: "a_platform" %>

  <hr>
  <%= render 'packages/package_form', event: @event %> # <--- Nested form here.

  <div class="actions" id="listen-for-hover">
    <%= f.submit 'Save', class: 'btn btn-primary', :id => "event-create-button"  %>
  </div>
<% end %>


#_package_form.rb
<%= form_for([@event, @package]) do |f| %>
  <%= render 'shared/error_messages', object: f.object %>

  <%= f.hidden_field :kind, value: params[:kind] if f.object.new_record? %>

  <div class="form-group required">
    <%= f.label :Your_offer_?, "Your Offer?" %>
    <%= f.text_area :offering, class: 'form-control', :placeholder => "e.g. Your logo on stuff" %>
  </div>

  <div class="form-group">
    <%= f.label :notes %>
    <%= f.text_area :notes, class: 'form-control', :placeholder => "Notes here"  %>
  </div>

  <hr>

  <div class="actions">
    <%= f.submit 'Submit', class: 'btn btn-primary', id: 'package-submit-button', disabled: true %>
  </div>
<% end %>

以这种方式使用表单会导致此错误:

Extracted source (around line #3):
1  <%# remoteFlag = false %>
2  <%# remoteFlag = true if @event.title == "dummy event" %>
3  <%= form_for([@event, @package]) do |f| %>
4    <%= render 'shared/error_messages', object: f.object %>

显然这是因为@event还没有被创建,所以没有它的ID。制作这个嵌套表单的最佳方法是用于同时创建新事件/包(没有ID的新对象)或使用部分另一部分编辑现有(带有ID的现有对象)应用?

2 个答案:

答案 0 :(得分:0)

您可以使用&#39; build&#39;创建空包的方法&#39;在你的活动中#39; official RoR guides

上有一个例子

E.g。

<%= form_for([@event, @event.packages.build]) do |f| %>

答案 1 :(得分:0)

您需要添加

accepts_nested_attributes_for :package

到你的事件模型(在event.rb中)。然后,在事件控制器的“新”方法中,添加

@package = @event.packages.build

在表单中,您需要使用fields_for方法。这整个过程有一个很好的Railscast:http://railscasts.com/episodes/196-nested-model-form-part-1 如果您不熟悉Ryan Bates的Railscasts,我强烈建议您花一些时间与他们在一起。