如何停止 fields_for 嵌套表单复制添加的记录?

发布于 2024-11-25 18:15:45 字数 2158 浏览 1 评论 0原文

我有一个场地表,我使用场地编辑页面上的嵌套表单向每个场地添加优惠。但是,每次我添加新报价时,fields_for 表单都会保存输入的文本,并为要添加的另一个报价记录创建一个新的空白表单。

我只想要一张“添加新报价”表格,而不是为每条添加的记录添加一张表格。

没有添加优惠 - 这很好: 在此处输入图像描述

添加了一个报价 - 现在有 2 个“添加新报价”表格和一个不需要的空白报价部分:

在此处输入图像描述

这就是我希望添加一个优惠后的样子:< /b> 在此处输入图像描述

新的空白报价表格和空白部分的数量随着控制器中内置的数量而变化(在分钟其 1)

场地控制器

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

场地模型

class Venue < ActiveRecord::Base
  has_many :offers
  accepts_nested_attributes_for :offers
end

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

那么我怎样才能在场地上添加新的报价表格编辑页面,它可以让我添加一个新的报价然后将表格清空以便可以再次使用?另外,有什么方法可以防止创建空白报价部分吗?

非常感谢您的帮助,非常感谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

公布 2024-12-02 18:15:45
class VenuesController < ApplicationController   
  def edit
    @venue = Venue.find(params[:id])
  end
  ...
end

场地 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 %>
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 %>
皇甫轩 2024-12-02 18:15:45

我不确定这是否有效,但您尝试过以下方法吗?

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

我认为问题在于您向其传递了符号 :offers,这会导致 fields for 为所有优惠创建字段,但您也应该能够仅将特定对象传递给 fields_for 方法。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文