2012-07-02 124 views
-1

我在我的Rails應用程序中使用了accep_nested_attributes_for。我有三種模式:專輯has_many:照片和每個:照片has_and_belongs_to_many:標記。用戶可以使用paperclip和jquery.multifile.js在專輯中添加更多照片。每件事情都可以正常工作,但主要問題是散列沒有正確創建。accepted_nested_attributes_for with complex association

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 %> 
+0

我認爲散列的格式是由您的fields_for參數決定的。你可以在表單中分享你的視圖代碼嗎? – yuklai

回答

0

您是否嘗試過使用嵌套表格寶石? Give it a try

它具有多個嵌套窗體的非常簡單的方法。

它適用於我。