Ajax, Multiple Attachments and Paperclip question.
Posted
by dustmoo
on Stack Overflow
See other posts from Stack Overflow
or by dustmoo
Published on 2010-04-28T19:25:54Z
Indexed on
2010/04/29
20:17 UTC
Read the original article
Hit count: 312
Alright everyone this is a bit of a complicated setup so if I need to clarify the question just let me know.
I have a model:
class IconSet < ActiveRecord::Base
has_many :icon_graphics
end
This Model has many icongraphics:
class IconGraphic < ActiveRecord::Base
belongs_to :icon_set
has_attached_file :icon
has_attached_file :flagged
end
As you can see, IconGraphic has two attached files, basically two different versions of the icon that I want to load.
Now, this setup is working okay if I edit the icongraphic's individually, however, for ease of use, I have all the icon graphics editable under the IconSet. When you edit the icon set the form loads a partial for the 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 %>
This is based largely off of Ryan's Complex Forms Railscast.
The partial loads the file_field forms:
<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>
Now, this is looking fine when I add new icons, and fill both fields. However, if I edit the IconSet after the fact, and perhaps try to replace the icon with a new one, or if I uploaded only one of the set and try to add the second attachment, paperclip doesn't put the attachments with the right IconGraphic Model.
It seems that even though I have the IconGraphic ID in each partial,
<%= icon_form.hidden_field :id, :index => nil %>
it seems that paperclip either creates a new IconGraphic or attaches it to the wrong one.
This all happens when you save the IconSet, which is setup to save the IconGraphic attributes.
I know this is complicated.. I may just have to go to editing each icon individually, but if anyone can help, I would appreciate it.
© Stack Overflow or respective owner