Topic: attachment_fu update/correct setup

I'm trying to use attachment_fu with other models and run into problems. Does anyone know any tutorial and documentation that goes into having e.g. a separate model class that connects to the attachment_fu class etc.?

Also, the edit and update don't seem to work other than deleting the file first and then entering a new one. I'd rather have my edit.rhtml for my e.g. artist class handling the deletion/updating of the picture.

Is there anything out there that goes into that as well?

If not, I'm happy to elaborate on where I am stuck but I figured that there must be plenty of documentation around that already covered those exact problems. I'm just not able to find it. smile

Any help is appreciated.

Jean-Marie

Re: attachment_fu update/correct setup

the only tut I know is Clarkware's one

--

Re: attachment_fu update/correct setup

BasicMind,

That's the one I'm working off. It's fine as long as you just need to upload pictures and delete them.

I decided to combine my data model with the attachment model:

class CreateArtists < ActiveRecord::Migration
  def self.up
    create_table :artists do |t|
      t.column :parent_id, :integer
      t.column :content_type, :string
      t.column :filename, :string
      t.column :thumbnail, :string
      t.column :size, :integer
      t.column :width, :integer
      t.column :height, :integer
      t.column :name, :string
    end
  end

  def self.down
    drop_table :artists
  end
end


And upload the picture in the artist_new view:

<h1>New artist</h1>

<%= error_messages_for :artist %>

<% form_for(:artist,
            :url => artists_path,
            :html => { :multipart => true }) do |f| %>
  <p>
    <b>Name</b><br />
    <%= f.text_field :name %>
  </p>
  <p>
    <b>Image</b><br />
    <%= f.file_field :uploaded_data %>
  </p>

  <p>
    <%= submit_tag "Create" %>
  </p>
<% end %>

<%= link_to 'Back', artists_path %>


But, for the edit.rhtml I would like to show the image and UPDATE any preexisting files. This code doesn't do it:

<h1>Editing artist</h1>

<%= error_messages_for :artist %>

<% form_for(:artist, :url => artist_path(@artist), :html => { :method => :put }) do |f| %>
    <p>
      <b>Name</b><br />
      <%= f.text_field :name %>
    </p>

  <p>
    <b>Filename</b><br />
    <%= f.text_field :filename %>
    <%= f.file_field :uploaded_data %>
  </p>

  <p>
    <%= submit_tag "Update" %>
  </p>
<% end %>

<%= link_to 'Show', artist_path(@artist) %> |
<%= link_to 'Back', artists_path %>


The image_tag throws an exception: private method `split' called for :filename:Symbol

And without the image tag, the update doesn't work, meaning that I can't upload another file that replaces the old one.

I'm thinking that I need to make some changes in the controller in order for the update to work but I just can't figure out how.

Maybe you have an idea?

Jean-Marie

Re: attachment_fu update/correct setup

Could I use the destroy_file method inside file_system_backend.rb of the attachment_fu plugin and delete the existing file(s) first before the new one gets created?

How would I do that. I assume I use some before_filters etc. But when and where is the questions. E.g. if the new image doesn't upload well that I shouldn't delete the old one and keep all the records clean.

Jean-Marie

Re: attachment_fu update/correct setup

I also found this inside attachment_fu.rb

# Callback after an attachment has been saved either to the file system or the DB.
      # Only called if the file has been changed, not necessarily if the record is updated.
      #
      #   class Foo < ActiveRecord::Base
      #     acts_as_attachment
      #     after_attachment_saved do |record|
      #       ...
      #     end
      #   end
      def after_attachment_saved(&block)
        write_inheritable_array(:after_attachment_saved, [block])
      end

Here it seems like that updating the file was intended to work, but it doesn't.