如何在此链接之后(而不是之前)渲染生成的字段并抛出link_to_add_association?

时间:2019-01-21 15:53:00

标签: ruby-on-rails ruby nested-attributes simple-form-for

我在simple_form_for中的link_to_add_association遇到了麻烦:在link_to_add_association之前添加了额外的字段,并且在该链接之后已经存在呈现的字段。 Google对此无话可说,所以希望对您有所帮助...

这是我的代码:

= link_to_add_fields "Add image", f, :attachments
.attach
    = f.simple_fields_for :attachments, html: {multipart: true} do |d|
      = render "attachment_fields", {f: d, item: "Main"}

这是我的Attachment_fields表单:

.nested-fields
    = f.input :id, as: :hidden
    = f.input :file, as: :file, label: false, input_html: {value: f.object.file || ""}
    = f.input :_destroy, as: :boolean, label: "Delete", checked_value: "1", unchecked_value: ""
    = f.simple_fields_for :metadata do |field|
        = field.input :section, as: :hidden, input_html: {value: f.object.metadata["section"]}
        = field.input :comment, as: :text, label: "Comment", input_html: {value: f.object.metadata["comment"]}

我已经尝试编写自己的link_to_add_fields了,但是看起来却在做同样的事情:

  def link_to_add_fields(name, f, association)
    new_object = f.object.send(association).klass.new
    id = new_object.object_id
    fields = f.fields_for(association, new_object, child_index: id) do |builder|
      render(association.to_s.singularize + "_fields", f: builder)
    end
    link_to(name, '#', class: "add_image_fields", data: {id: id, fields: fields.gsub("\n", "")})
  end

0 个答案:

没有答案