Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I do have a project in the works that is successfully using Paperclip and polymorphic associations. Let me show you what I have, and maybe you can apply it to your project:</p> <pre><code>class Song &lt; ActiveRecord::Base ... has_one :artwork, :as =&gt; :artable, :dependent =&gt; :destroy accepts_nested_attributes_for :artwork ... end class Album &lt; ActiveRecord::Base ... has_one :artwork, :as =&gt; :artable, :dependent =&gt; :destroy accepts_nested_attributes_for :artwork ... end class Artwork &lt; ActiveRecord::Base belongs_to :artable, :polymorphic =&gt; true attr_accessible :artwork_content_type, :artwork_file_name, :artwork_file_size, :artwork # Paperclip has_attached_file :artwork, :styles =&gt; { :small =&gt; "100", :full =&gt; "400" } validates_attachment_content_type :artwork, :content_type =&gt; 'image/jpeg' end </code></pre> <p>the songs form and the albums form include this as a partial:</p> <pre><code>&lt;div class="field"&gt; &lt;%= f.fields_for :artwork do |artwork_fields| %&gt; &lt;%= artwork_fields.label :artwork %&gt;&lt;br /&gt; &lt;%= artwork_fields.file_field :artwork %&gt; &lt;% end %&gt; </code></pre> <p></p> <p>don't forget to include :html => { :multipart => true } with the form</p> <p>artworks_controller.rb</p> <pre><code>class ArtworksController &lt; ApplicationController def create @artwork = Artwork.new(params[:artwork]) if @artwork.save redirect_to @artwork.artable, notice: 'Artwork was successfully created.' else redirect_to @artwork.artable, notice: 'An error ocurred.' end end end </code></pre> <p>and finally, an excerpt from songs_controller.rb:</p> <pre><code>def new @song = Song.new @song.build_artwork end </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload