Pergunta

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: enter image description here

one offer added - now theres 2 'add new offer' forms and an unwanted blank offer partial:

enter image description here

This is what I want it look like after adding one offer: enter image description here

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!

Foi útil?

Solução

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

Outras dicas

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top