Hi all,

I have paperclip running in my rails environment. I setup a new model
just for paperclip called Assets so that I could upload multiple files
per item (even though I'm only using 1 attachment per item at the
moment). Everything works except for the following:

Updating an item record with no attachment in the form but having
PREVIOUSLY attached an asset while creating or updating. The only way
that this works is if I create an item with no attachment and update the
same item record with no attachment OR if I created an item record with
no attachment and then update the same item record with an attachment.

Basically - Once an asset has been attached to an item record, updating
with no attachment yields the following error:


"ActiveRecord::RecordInvalid in ItemsController#update

Validation failed: Attachment file name can't be blank, Attachment file
size can't be blank"


There are validation errors from the Assets Model for the presence of
:attachment_file_name and :attachment_file_size.



I have the below code in my item model:

[code]
after_save create_or_update_assets!

  def create_or_update_assets!
    # return if there are no asset_attributes
    return true if asset_attributes.nil? || asset_attributes.empty?

    # pull the attributes from the instance variable
    asset_attributes.each do |attributes_hash|
      attributes_hash.symbolize_keys!

      # if atts_hash has an id (asset already exists). If there's no id
then this is a new asset.
      asset = attributes_hash[:id].blank? ? assets.build : assets.detect
{ |product| product.id == attributes_hash[:id].to_i }

      asset.attachment = attributes_hash[:attachment] # adds the
attributes to the asset

      asset.save! # saves the asset
    end
  end
[/code]


my view model looks like this:

[code]
<%- for asset in @item.assets do -%>
  <% f.fields_for("asset_attributes[]", asset) do |asset_fields| %>
    <p>
      <%= asset_fields.label :attachment, "File" %><br />
      <%= asset_fields.file_field :attachment, :index => nil %><br />
      <%= asset_fields.hidden_field :id, :index => nil unless(
asset.new_record? ) %>
    </p>
  <%- end -%>
<%- end -%>
[/code]


I'm positive this has to be a lack of some extra checking in my
create_or_update_assets! method. Any help is GREATLY appreciated. Please
let me know if you need any extra info to help solve this.


Many thanks,
Tony
-- 
Posted via http://www.ruby-forum.com/.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to