问题描述
我有一张表格,可以从场地编辑页面的场地增加优惠。
一旦提交表单,新创建的优惠将以部分显示,而不是表单清算,以允许输入另一个优惠,则会创建另一个表单。 p>
所以如果我输入5个优惠,我有6个添加新的优惠表单,其中5个添加了保存的详细信息。
我的代码如下:
场地模型
class Venue< ActiveRecord :: Base
has_many:提供
acceptance_nested_attributes_for:offer,:reject_if => lambda {| a |一[:标题] .blank? }
end
编辑 - 场地控制器
class VenuesController< ApplicationController
protect_from_forgery:only => [:update,:delete,:create]
load_and_authorize_resource
def index
如果
@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.new params [:场]
如果@ 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。 offer.build}
end
def update
@venue = Venue.find(params [:id])
@ venue.attributes = params [:venue]
如果@ 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])。
flash [:notice] ='Venue deleted successfully'
redirect_to venues_path
end
end
场地edit.html.erb
<% = form_for @venue do | f | %GT;
< div class =edit_venue_details>
< h2 class =venue_show_orange>优惠< / h2>
<%if @ venue.offers.count.zero? %GT;
< div class =no_reviews>
还没有报价。
< / div>
<%else%>
<%= render:partial => 'offer / offer',:collection => @ venue.offers%>
<%end%>
< div class =clearall>< / div>
< / div>
< h2 class =edit_venue_sub_header>添加新的优惠< / h2>
<%= f.fields_for:offer do | offer | %GT;
< p class =edit_venue> title:< br>
<%= offer.text_field:title,:class => edit_venue_input%>< / p>
<%end%>
< button class =submit_buttontype =submit>保存更改< / button>
<%end%>
如何在提交时阻止创建新表单?
感谢任何帮助,非常感谢!
编辑
:
def new
@venue = Venue.new
1.times {@ venue.offers。 build}
end
def create
@venue = Venue.new params [:places]
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。 offer.build}
end
def update
@venue = Venue.find(params [:id])
if @ venue.update_attributes(params [:places ])
flash [:notice] ='成功更新'
redirect_to edit_venue_path(@venue)
end
end
I have a table of venues where each can have many offers.
I have a form which adds offers to venues from the venues edit page.
Once I submit the form the newly created offer is shown as a partial but instead of the form clearing to allow another offer to be entered, another form is created.
So if I enter 5 offers I have 6 add new offer forms, 5 of them with the details entered into the saved ones.
My code looks like this:
venue model
class Venue < ActiveRecord::Base
has_many :offers
accepts_nested_attributes_for :offers, :reject_if => lambda { |a| a[:title].blank? }
end
edit - venue controller
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
venues 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 %>
How can I prevent a new form being created on submit?
Thanks for any help its much appreciated!
edit
Try this out for your controller:
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
这篇关于如何停止使用fields_for重复的表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!