Ajax,多个附件和回形针问题

时间:2010-04-28 19:25:54

标签: ruby-on-rails ajax paperclip

大家好,这是一个复杂的设置,所以如果我需要澄清这个问题,请告诉我。

我有一个模特:

class IconSet < ActiveRecord::Base
  has_many :icon_graphics
end

这个模型有许多icongraphics:

class IconGraphic < ActiveRecord::Base
  belongs_to :icon_set
  has_attached_file :icon
  has_attached_file :flagged
end

正如您所看到的,IconGraphic有两个附加文件,基本上是我要加载的两个不同版本的图标。

现在,如果我单独编辑icongraphic,这个设置工作正常,但是,为了便于使用,我在IconSet下可以编辑所有图标图形。编辑图标集时,表单会加载icongraphics的部分内容:

<% form_for @icon_set, :html => {:class => 'nice', :multipart => true} do |f| %>
<fieldset>  
  <%= f.error_messages %>
  <p>
    <%= f.label :name %>
    <%= f.text_field :name, :class => "text_input" %>
  </p>
    <!-- Loaded Partial for icongraphics -->
    <div id="icon_graphics">
        <%= render :partial => 'icon_graphic', :collection => @icon_set.icon_graphics %>        
    </div>
    <div class="add_link">
    <%= link_to_function "Add an Icon" do |page|
        page.insert_html :bottom, :icon_graphics, :partial => 'icon_graphic', :object => IconGraphic.new
    end %>
    </div>
  <p><%= f.submit "Submit" %></p>
</fieldset>
<% end %>

这主要基于Ryan的Complex Forms Railscast

部分加载file_field表单:

<div class="icon_graphic">
<% fields_for "icon_set[icon_graphic_attributes][]", icon_graphic do |icon_form|-%>
    <%- if icon_graphic.new_record? -%>
        <strong>Upload Icon: </strong><%= icon_form.file_field :icon, :index => nil %><br/>
        <strong>Upload Flagged Icon: </strong><%= icon_form.file_field :flagged, :index => nil %>
        <%= link_to_function image_tag('remove_16.png'), "this.up('.icon_graphic').remove()"%><br/>
    <% else -%>
        <%= image_tag icon_graphic.icon.url %><br/>
        <strong>Replace <%= icon_graphic.icon_file_name %>: </strong><%= icon_form.file_field :icon, :index => nil %><br />
        <% if icon_graphic.flagged_file_name.blank? -%>
        <strong>Upload Flagged Icon: </strong><%= icon_form.file_field :flagged, :index => nil %>
        <% else -%>
        <strong>Replace <%= icon_graphic.flagged_file_name %>: </strong><%= icon_form.file_field :flagged, :index => nil %>
        <%= icon_form.hidden_field :flagged, :index => nil %>
        <% end -%>
        <%= link_to_function image_tag('remove_16.png'), "mark_for_destroy(this, '.icon_graphic')"%><br/>
        <%= icon_form.hidden_field :id, :index => nil %>
        <%= icon_form.hidden_field :icon, :index => nil %>
        <%= icon_form.hidden_field :should_destroy, :index => nil, :class => 'should_destroy' %>
        <br/><br/>
    <%- end -%>
<% end -%>
</div>

现在,当我添加新图标并填充这两个字段时,这看起来很好。但是,如果我在事后编辑IconSet,并且可能尝试用新图标替换图标,或者如果我只上传了其中一个并尝试添加第二个附件,则回形针不会将附件放在右边IconGraphic模型。

似乎即使我在每个部分中都有IconGraphic ID,

<%= icon_form.hidden_field :id, :index => nil %>

似乎回形针要么创建一个新的IconGraphic,要么将它附加到错误的那个。

当你保存IconSet时会发生这一切,IconSet被设置为保存IconGraphic属性。

我知道这很复杂..我可能只需要单独编辑每个图标,但如果有人可以提供帮助,我将不胜感激。

1 个答案:

答案 0 :(得分:1)

好吧我明白了。问题是每个图标的ID需要在上传字段之前,而不是在:

之后
<% else -%>
      <!-- Moved the hidden fields before the upload fields -->
        <%= icon_form.hidden_field :id, :index => nil %>
        <%= icon_form.hidden_field :icon, :index => nil %>
        <%= icon_form.hidden_field :flagged, :index => nil unless icon_graphic.flagged_file_name.blank? %>
        <%= image_tag icon_graphic.icon.url %><%= image_tag icon_graphic.flagged.url unless icon_graphic.flagged_file_name.blank? %><br/>
        <strong>Replace <%= icon_graphic.icon_file_name %>: </strong><%= icon_form.file_field :icon, :index => nil %><br />
        <% if icon_graphic.flagged_file_name.blank? -%>
        <strong>Upload Flagged Icon: </strong><%= icon_form.file_field :flagged, :index => nil %>
        <% else -%>
        <strong>Replace <%= icon_graphic.flagged_file_name %>: </strong><%= icon_form.file_field :flagged, :index => nil %>
        <% end -%>
        <%= link_to_function image_tag('remove_16.png'), "mark_for_destroy(this, '.icon_graphic')"%><br/>
        <%= icon_form.hidden_field :should_destroy, :index => nil, :class => 'should_destroy' %>
        <br/><br/>
<%- end -%>