Rails 3 polymorphic association with Carrierwave a

2019-03-11 13:28发布

问题:

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.

Here are my associations:

property.rb:

class Property < ActiveRecord::Base
  attr_accessible :image
  ...
  has_many :image, :as => :attachable
  ...
end

unit.rb

class Unit < ActiveRecord::Base
  attr_accessible :image
  ...
  has_many :image, :as => :attachable
end

image.rb

class Image < ActiveRecord::Base
  belongs_to :attachable, :polymorphic => true
  mount_uploader :image, PhotoUploader
end

properties_controller.rb:

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 => 'The property has been successfully updated.'
    else
        render "edit"
    end
end

Snippet from properties/_form.html.erb

<%= f.input :image, :label => 'Image:', :as => :file %>

Here is the error I get when submitting with an image attached:

undefined method `each' for #<ActionDispatch::Http::UploadedFile:0x00000102291bb8>

And here are the params:

{"utf8"=>"✓",
 "_method"=>"put",
 "authenticity_token"=>"lvB7EMdc7juip3gBZD3XhCLyiv1Vwq/hIFdb6f1MtIA=",
 "property"=>{"name"=>"Delaware Woods",
 "address"=>"",
 "city"=>"",
 "state"=>"",
 "postal_code"=>"",
 "description"=>"2 bedroom with large kitchen.  Garage available",
 "incentives"=>"",
 "active"=>"1",
 "feature_ids"=>[""],
 "user_ids"=>[""],
 "image"=>#<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=#<File:/tmp/RackMultipart20120608-3102-13f3pyv>>},
 "commit"=>"Update Property",
 "id"=>"18"}

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 images and not image. However when I do that I get an error:

Can't mass-assign protected attributes: image

I've tried updating my form to use fields_for as I've seen in other blogs like so:

<%= f.input :image, :label => "Photo", :as => :file %>

<% f.simple_fields_for :images do |images_form| %>
        <%= images_form.input :id, :as => :hidden %>
        <%= images_form.input :attachable_id, :as => :hidden %>
        <%= images_form.input :attachable_type, :as => :hidden %>
        <%= images_form.input :image, :as => :file %>
<% end %>

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 :(

回答1:

Since your models have_many :images (it should be :images, not :image), you'll want to use nested_forms in your views. You should set up accepts_nested_attributes_for :images on the unit and property models and change the attr_accessible from :image to :image_attributes.

Check out http://railscasts.com/episodes/196-nested-model-form-part-1 for a good guide on getting going with it.