Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8065869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:48:22+00:00 2026-06-05T11:48:22+00:00

I’m trying to set up a polymorphic association for photo uploads which are processed

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T11:48:24+00:00Added an answer on June 5, 2026 at 11:48 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Paperclip to handle profile photo uploads in my app. They upload
I am trying to understand how to use SyndicationItem to display feed which is
We're building an app, our first using Rails 3, and we're having to build
I have a text area in my form which accepts all possible characters from
I'm making a simple page using Google Maps API 3. My first. One marker
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.