Note that there are some explanatory texts on larger screens.

plurals
  1. PORails 3 polymorphic association with Carrierwave and Simple Form
    primarykey
    data
    text
    <p>I'm trying to set up a polymorphic association for photo uploads which are processed using Carrierwave. I'm using Simple Form to build my forms. I feel like the association is correct so I'm wondering if my problem is just something with the form or controller.</p> <p>Here are my associations:</p> <p>property.rb:</p> <pre><code>class Property &lt; ActiveRecord::Base attr_accessible :image ... has_many :image, :as =&gt; :attachable ... end </code></pre> <p>unit.rb</p> <pre><code>class Unit &lt; ActiveRecord::Base attr_accessible :image ... has_many :image, :as =&gt; :attachable end </code></pre> <p>image.rb</p> <pre><code>class Image &lt; ActiveRecord::Base belongs_to :attachable, :polymorphic =&gt; true mount_uploader :image, PhotoUploader end </code></pre> <p>properties_controller.rb:</p> <pre><code>def edit @property = Property.find params[:id] @property.image.build if @property.image.empty? end def update @property = Property.find params[:id] if @property.update_attributes params[:property] redirect_to admin_properties_path, :notice =&gt; 'The property has been successfully updated.' else render "edit" end end </code></pre> <p>Snippet from properties/_form.html.erb</p> <pre><code>&lt;%= f.input :image, :label =&gt; 'Image:', :as =&gt; :file %&gt; </code></pre> <p>Here is the error I get when submitting with an image attached:</p> <pre><code>undefined method `each' for #&lt;ActionDispatch::Http::UploadedFile:0x00000102291bb8&gt; </code></pre> <p>And here are the params:</p> <pre><code>{"utf8"=&gt;"✓", "_method"=&gt;"put", "authenticity_token"=&gt;"lvB7EMdc7juip3gBZD3XhCLyiv1Vwq/hIFdb6f1MtIA=", "property"=&gt;{"name"=&gt;"Delaware Woods", "address"=&gt;"", "city"=&gt;"", "state"=&gt;"", "postal_code"=&gt;"", "description"=&gt;"2 bedroom with large kitchen. Garage available", "incentives"=&gt;"", "active"=&gt;"1", "feature_ids"=&gt;[""], "user_ids"=&gt;[""], "image"=&gt;#&lt;ActionDispatch::Http::UploadedFile:0x00000102291bb8 @original_filename="wallpaper-4331.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"property[image]\"; filename=\"wallpaper-4331.jpg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#&lt;File:/tmp/RackMultipart20120608-3102-13f3pyv&gt;&gt;}, "commit"=&gt;"Update Property", "id"=&gt;"18"} </code></pre> <p>I'm looking everywhere for help on polymorphic associations and am getting nowhere. I've seen simple examples that look pretty straight forward. One thing I've noticed is that it seems like in a lot of the examples the has_many association in my case should be <code>images</code> and not <code>image</code>. However when I do that I get an error:</p> <pre><code>Can't mass-assign protected attributes: image </code></pre> <p>I've tried updating my form to use fields_for as I've seen in other blogs like so:</p> <pre><code>&lt;%= f.input :image, :label =&gt; "Photo", :as =&gt; :file %&gt; &lt;% f.simple_fields_for :images do |images_form| %&gt; &lt;%= images_form.input :id, :as =&gt; :hidden %&gt; &lt;%= images_form.input :attachable_id, :as =&gt; :hidden %&gt; &lt;%= images_form.input :attachable_type, :as =&gt; :hidden %&gt; &lt;%= images_form.input :image, :as =&gt; :file %&gt; &lt;% end %&gt; </code></pre> <p>All I know is I'm having a heck of a time getting this to work. I'm pretty new to Rails so even debugging it is difficult. It doesn't help that the debugger doesn't really work in 3.2 :(</p>
    singulars
    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.
 

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