我有一个场地表,我正在使用场地编辑页面上的嵌套表单向每个场地添加优惠。但是,每次我添加新报价时,fields_for 表单都会保存输入的文本,并为要添加的另一个报价记录创建一个新的空白表单。
我只想要一个“添加新报价”表格,而不是添加的每条记录。
没有添加优惠 - 这很好:
添加了一个报价 - 现在有 2 个“添加新报价”表格和一个不需要的空白报价部分:
这是我希望添加一个报价后的样子:
新的空白报价表格和空白部分的数量随着控制器中内置的数量而变化(在其分钟内为 1)
场地控制器
class VenuesController < ApplicationController
def edit
@venue = Venue.find(params[:id])
1.times { @venue.offers.build }
end
def update
@venue = Venue.find(params[:id])
if @venue.update_attributes(params[:venue])
flash[:notice] = 'Venue updated successfully'
redirect_to :back
end
end
end
场地模型
class Venue < ActiveRecord::Base
has_many :offers
accepts_nested_attributes_for :offers
end
场地 edit.html.erb
<%= form_for @venue do |f| %>
<h2 class="venue_show_orange">Offers</h2>
<div class="edit_venue_details">
<% if @venue.offers.count.zero? %>
<div class="no_offers">
No offers added yet.
</div>
<% else %>
<%= render :partial => 'offers/offer', :collection => @venue.offers %>
<% end %>
<div class="clearall"></div>
<h2 class="edit_venue_sub_header">Add a new offer</h2>
<%= f.fields_for :offers do |offer| %>
<p class="edit_venue">title: <br>
<%= offer.text_field :title, :class => "edit_venue_input" %></p>
<% end %>
</div>
<button class="submit_button" type="submit"> Save changes</button>
<% end %>
那么,我怎样才能在场地编辑页面上只添加一个添加新报价表,这样我就可以添加一个新报价,然后将表格清空以便再次使用?另外,有什么方法可以防止创建空白报价部分吗?
非常感谢任何帮助,非常感谢!