-1

我在我的一个 Rails 应用程序中使用了accepts_nested_attributes_for。我有三个模型 :album has_many :photo 和每个 :photo has_and_belongs_to_many :tag。用户可以使用回形针和 jquery.multifile.js 在相册中添加更多照片。一切正常,但主要问题是未正确创建哈希。

album => {:name => "", :body => "", :photos_attributes => {"tags_attributes" => {:name => "abc"}, "photo" => {"file" => "tempfile"}}}

但是我需要。

album => {:name => "", :body => "", :photos_attributes => "photo" => {"file" => "tempfile", "tags_attributes" => {:name => "abc"}}}}

我的观点是:请考虑图库 => 相册和附件 => 照片

<%= form_for(@gallery, :html => {:multipart => true}) do |f| %>
    <div id = "gallery_name_formField">
        <%= f.label "Gallery Name:" %><br />
        <%= f.text_field :name, :name => "gallery[name]" %><br />
    </div>
    <div id = "gallery_body_formField">
        <%= f.label "Gallery Description:" %><br />
        <%= f.text_area :body, :name => "gallery[body]" %><br />
    </div>
    <%= f.fields_for :attachments do |af| %>
        <div id = "gallery_file_formField">
            <%= af.label "Select The Attachment:" %><br />
            <%= af.file_field(:attachment, :name => "gallery[attachments_attributes][][attachment]")%>
        </div>
        <div id="tags">
            <%= af.fields_for :tags do |tf| %>
                <%= tf.label :name, "Tag:" %><br />
                <%= tf.text_field :name, :id => "mySingleField_0", :name => "gallery[attachments_attributes][][tags_attributes][][name]" %>
            <% end %>
        </div>
    <% end %>

    <div class="attachment_submit_formField">
        <%= f.submit(:id => "create_gallery") %>
    </div>
<% end %>
4

1 回答 1

0

您是否尝试过使用嵌套形式的 gem?试试看

拥有多个嵌套表单的非常简单的方法。

这个对我有用。

于 2012-07-03T01:11:11.040 回答