Achille Pinson
2008-Jul-17 10:36 UTC
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: ------------------- <h1>Création d''une nouvelle association</h1> <% form_tag :action => ''create'' do %> <%= error_messages_for :association %> <%= error_messages_for :image %> <p><label for="association_nom">Nom</label><br/> <%= text_field ''association'', ''nom'' %></p> <p><label for="association_description">Contenu de la page:</label><br/> <%= text_area ''association'', ''description'' %></p> <%= 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 ! -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Peter De Berdt
2008-Jul-18 07:12 UTC
Re: attachment_fu and polymorphism. Two models in one form.
On 17 Jul 2008, at 12:36, Achille Pinson wrote:> <% form_tag :action => ''create'' do %> > > <%= error_messages_for :association %> > <%= error_messages_for :image %> > > <p><label for="association_nom">Nom</label><br/> > <%= text_field ''association'', ''nom'' %></p> > > <p><label for="association_description">Contenu de la > page:</label><br/> > <%= text_area ''association'', ''description'' %></p> > > <%= 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.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 --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---