I have a table of venues and I'm adding offers to each venue using a nested form on the venues edit page. However, each time I add a new offer the fields_for form saves the text entered and creates a new blank form for another offer record to be added.
I just want the one 'add new offer' form not one for each record added.
no offers added - this is fine:
one offer added - now theres 2 'add new offer' forms and an unwanted blank offer partial:
This is what I want it look like after adding one offer:
The number of new blank offer forms and blank partials changes with the number being built in the controller (at the minute its 1)
venues controller
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
venues model
class Venue < ActiveRecord::Base
has_many :offers
accepts_nested_attributes_for :offers
end
venues 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 %>
So how can I have just the one add new offer form on the venues edit page, which lets me add a new offer then blanks the form so it can be used again? Also, is there any way to prevent the blank offer partials being created?
Thanks very much for any help its much appreciated!
class VenuesController < ApplicationController
def edit
@venue = Venue.find(params[:id])
end
...
end
venues edit.html.erb
<%= f.fields_for :offers, @venue.offers.build do |offer| %>
<p class="edit_venue">title: <br>
<%= offer.text_field :title, :class => "edit_venue_input" %></p>
<% end %>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With