Attachment_fu and polymorphism. Two models in one form

Hi!

I just discover attachment_fu a few days ago.
I made it work in a simple form.

Now I want to use a polymorphic relation.
I want to attach a picture to a ‘association’ in the ‘association’ form.

Models:

class Association < ActiveRecord::Base
has_one :image, :as => :attachable, :dependent => :destroy
end

class Image < ActiveRecord::Base

belongs_to :attachable, :polymorphic => true

has_attachment :content_type => :image,
:storage => :file_system,
:max_size => 500.kilobytes,
:resize_to => ‘320x200’,
:thumbnails => { :thumb => [50,50] },
:processor => :Rmagick

validates_as_attachment

end

The controller:

[code=]def new
@association = Association.new
end

def create
@association = Association.new(params[:association])
image = @association.create_image(params[:image])
if @association.save and image.save
flash[:notice] = ‘Association was successfully created.’
redirect_to :action => ‘list’
else
render :action => ‘new’
end
end

The view:

Création d'une nouvelle association

<% form_tag :action => ‘create’ do %>

    <%= error_messages_for :association %>
    <%= error_messages_for :image %>

Nom
<%= text_field 'association', 'nom' %>

    <p><label for="association_description">Contenu de la

page:

<%= text_area ‘association’, ‘description’ %>

    <%= file_field 'image', :uploaded_data %>

<%= submit_tag “Create” %>

<% end %>

<%= link_to ‘Back’, :action => ‘list’ %>

But I get the error:
undefined method `content_type’ for “2083186.jpg”:String

I don’t understand how i can fix this.

Thanxs !

On 17 Jul 2008, at 12:36, Achille P. wrote:

   <%= text_area 'association', 'description'  %></p>

undefined method `content_type’ for “2083186.jpg”:String

I don’t understand how i can fix this.

File uploads need a multipart form:

<% form_tag :action => ‘create’, :html => {:multipart => true} do %>

This is clearly stated on line 115 of the attachment_fu README.

Best regards

Peter De Berdt

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs