ruby-on-rails – 如何在添加了记录的情况下停止使用fields_for
发布时间:2020-12-17 03:27:48 所属栏目:百科 来源:网络整理
导读:我有一个场地表,我在场地编辑页面上使用嵌套表格为每个场地添加优惠.但是,每次添加新商品时,fields_for表单都会保存输入的文本,并为要添加的其他商品记录创建新的空白表单. 我只想为每个添加的记录添加一个“添加新优惠”表单. 没有添加任何优惠 – 这很好:
我有一个场地表,我在场地编辑页面上使用嵌套表格为每个场地添加优惠.但是,每次添加新商品时,fields_for表单都会保存输入的文本,并为要添加的其他商品记录创建新的空白表单.
我只想为每个添加的记录添加一个“添加新优惠”表单. 没有添加任何优惠 – 这很好: 添加了一个优惠 – 现在theres 2’添加新优惠’表单和不需要的空白优惠部分: 添加一个优惠后,这就是我想要的样子: 新的空白报价表格和空白部分的数量随着控制器中内置的数字的变化而变化(在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 %> 那么我怎样才能在场地编辑页面上添加一个新的报价表单,这样我就可以添加一个新的报价,然后将表格空白以便再次使用?另外,有没有办法防止创建空白报价部分? 非常感谢任何帮助,非常感谢! 解决方法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 %> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |