0
我有2個模型,Service和Subservice。Fields_for每次我無限存儲
class Service < ActiveRecord::Base
mount_uploader :teaser, TeaserUploader
has_many :subservices
accepts_nested_attributes_for :subservices, allow_destroy: true
end
class Subservice < ActiveRecord::Base
mount_uploader :subicon, SubiconUploader
belongs_to :service, dependent: :destroy
validates_presence_of :service
end
我希望每個服務有很多的子服務,這是我通過這種形式添加:
<%= simple_form_for @service do |f| %>
<%= f.input :name %>
<%= f.fields_for :subservices do |builder| %>
<%= builder.input :name, label: "Nombre Servicio", class: "form-control" %>
<%= builder.input :description, label: "Descripción del servicio" %>
<%= builder.input :subicon, label: "Icono" %>
<% end %>
<%= f.button :submit %>
<% end %>
它工作正常,但每次我編輯一個新的子服務,它就會被複制,而不是保存舊的。 它向窗體添加了一個新的子服務,而不是僅僅編輯實際的服務。
# GET /services/1/edit
def edit
@service = Service.find(params[:id])
@service.subservices.build
end
謝謝!