activerecord - Rails 3 polymorphic association with Carrierwave and Simple Form -
i'm trying set polymorphic association photo uploads processed using carrierwave. i'm using simple form build forms. feel association correct i'm wondering if problem form or controller.
here 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 updated.' else render "edit" end end
snippet properties/_form.html.erb
<%= f.input :image, :label => 'image:', :as => :file %>
here error when submitting image attached:
undefined method `each' #<actiondispatch::http::uploadedfile:0x00000102291bb8>
and here params:
{"utf8"=>"✓", "_method"=>"put", "authenticity_token"=>"lvb7emdc7juip3gbzd3xhclyiv1vwq/hifdb6f1mtia=", "property"=>{"name"=>"delaware woods", "address"=>"", "city"=>"", "state"=>"", "postal_code"=>"", "description"=>"2 bedroom 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 on polymorphic associations , getting nowhere. i've seen simple examples pretty straight forward. 1 thing i've noticed seems in lot of examples has_many association in case should images
, not image
. when error:
can't mass-assign protected attributes: image
i've tried updating form use fields_for i've seen in other blogs so:
<%= f.input :image, :label => "photo", :as => :file %> <% f.simple_fields_for :images |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 know i'm having heck of time getting work. i'm pretty new rails debugging difficult. doesn't debugger doesn't work in 3.2 :(
since models have_many :images (it should :images, not :image), you'll want use nested_forms in views. should set accepts_nested_attributes_for :images on unit , property models , change attr_accessible :image :image_attributes.
check out http://railscasts.com/episodes/196-nested-model-form-part-1 guide on getting going it.
Comments
Post a Comment