开发者

How can I stop a fields_for nested form duplicating with records added?

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:

How can I stop a fields_for nested form duplicating with records added?

one offer added - now theres 2 'ad开发者_Python百科d new offer' forms and an unwanted blank offer partial:

How can I stop a fields_for nested form duplicating with records added?

This is what I want it look like after adding one offer:

How can I stop a fields_for nested form duplicating with records added?

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 %>


I'm not sure if this will work, but have you tried the following?

<%= f.fields_for @venue.offers.last do |offer| %>

I think the problem is that you pass it the symbol :offers, which cause fields for to create fields for all the offers, but you should be able to pass only a specific object to the fields_for method as well.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜