我有2个型号,服务和子服务。
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
谢谢!