我有一个场地表,每个场地都有许多优惠。
我有一个表格,可以从场地编辑页面向场馆添加优惠。
一旦我提交表单,新创建的报价将显示为部分报价,而不是表格清算以允许输入其他报价,则会创建另一个报表。
因此,如果我输入5个优惠,我会添加6个新优惠表格,其中5个会在保存的表格中输入详细信息。
我的代码如下所示:
场地模型
class Venue < ActiveRecord::Base
has_many :offers
accepts_nested_attributes_for :offers, :reject_if => lambda { |a| a[:title].blank? }
end
编辑 - 场地控制器
class VenuesController < ApplicationController
protect_from_forgery :only => [:update, :delete, :create]
load_and_authorize_resource
def index
if
@venues = Venue.with_type(params[:venuetypes]).with_area(params[:areas]).order("average_rating DESC").all
else
@venues = Venue.all
end
@venues = @venues.paginate :per_page => 15, :page => params[:page]
end
def new
@venue = Venue.new
end
def create
@venue = Venue.new params[:venue]
if @venue.save
flash[:notice] = 'Venue added'
redirect_to venues_path
else
render :action => :new
end
end
def edit
@venue = Venue.find(params[:id])
1.times { @venue.offers.build }
end
def update
@venue = Venue.find(params[:id])
@venue.attributes = params[:venue]
if @venue.save!
flash[:notice] = 'Venue updated successfully'
redirect_to :back
end
end
def show
@venue = Venue.find(params[:id])
@review = Review.new
end
def destroy
Venue.find(params[:id]).destroy
flash[:notice] = 'Venue deleted successfully'
redirect_to venues_path
end
end
场地edit.html.erb
<%= form_for @venue do |f| %>
<div class="edit_venue_details">
<h2 class="venue_show_orange">Offers</h2>
<% if @venue.offers.count.zero? %>
<div class="no_reviews">
No offers added yet.
</div>
<% else %>
<%= render :partial => 'offers/offer', :collection => @venue.offers %>
<% end %>
<div class="clearall"></div>
</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 %>
<button class="submit_button" type="submit"> Save changes</button>
<% end %>
如何阻止在提交时创建新表单?
感谢任何帮助,非常感谢!
修改
答案 0 :(得分:0)
为您的控制器试试这个:
def new
@venue = Venue.new
1.times { @venue.offers.build }
end
def create
@venue = Venue.new params[:venue]
if @venue.save
flash[:notice] = 'Venue added'
redirect_to edit_venue_path
else
render :action => :new
end
end
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 edit_venue_path(@venue)
end
end