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 7437895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:28:10+00:00 2026-05-29T10:28:10+00:00

I suspect the problem lies in the way I am creating the polymorphic image

  • 0

I suspect the problem lies in the way I am creating the polymorphic image attribute. I am using fields_for in the form.
In this case a user can create a post and add an image using paperclip, storing it with S3.
I am using a polymorphic image model “post_image”:

class PostImage < ActiveRecord::Base
  belongs_to :imageable, :polymorphic => true
  #.merge(PAPERCLIP_OPS)
  has_attached_file :image, :styles => { :medium => "200x200>", :thumb => "50x50>" },
                            :storage => :s3, 
                            :s3_credentials => "#{Rails.root}/config/s3.yml", 
                            :path => "/:style/:id/:filename", 
                            :bucket => "zounds-dev"
  validates_attachment_presence :image
  validates_attachment_size :image, :less_than => 5.megabytes
end

Post Model:

class Post < ActiveRecord::Base
  has_many :post_images, :as => :imageable, :dependent => :destroy
  .
  .
  .
  accepts_nested_attributes_for :post_images, :reject_if => lambda { |t| t[:post_image].nil?}, :allow_destroy => true
end

New Post Form:

=form_for( setup_post(@post,current_user), :html => { :multipart => true}) do |f|
  %dl   
    =f.fields_for :post_images do |ff|
      =ff.file_field :image
    %dt.field=f.label :name
    %dd.field=f.text_field :name
    %dt.field=f.label :description
    %dd.field=f.text_area :description
    =f.fields_for :user do |u|
      =render "user_fields", :f => u
    =f.fields_for :assignments do |ff|
      =ff.check_box :_destroy, {:checked => ff.object.persisted?}, '0','1'
      =ff.label :_destroy, ff.object.group.name
      =ff.hidden_field :group_id
    .action=f.submit "Save Post"

The setup_post helper method used in the Post form_for: (the groups stuff isn’t relevant here)

  def setup_post(post, current_user)
    groups = current_user.groups_as_owner + current_user.groups_as_member
    (groups - post.groups).each do |group|
      post.assignments.build(:group => group)
    end
    post.assignments.sort_by {|x| x.group.name }
    post_image = post.post_images.build
    post
  end

Post controller:

  def new
    @user = User.find(params[:user_id])
   #  @post = @user.posts.build
   @post = Post.new
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @post }
    end
  end

def create
    @user = current_user
    @post = @user.posts.build(params[:post])
    .
    .
    .
end

I suspect the problem is that I am using fields_for for the post_image attribute, but I’ve looked all over and can’t figure out what the proper way to implement a polymorphic nested image attribute is.

I also did the s3sh amazon s3 console thing, and although I couldn’t upload an image because I couldn’t figure out how to pass in the right image path to the open() function, I connected to S3. My s3.yml file is set up correctly as well.

Thanks yall,
Brian

  • 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-05-29T10:28:11+00:00Added an answer on May 29, 2026 at 10:28 am

    The issue was with the reject_if in accepts_nested_attributes for the Post model

    accepts_nested_attributes_for :post_images, :reject_if => lambda { |t| t[:post_image].nil?}, :allow_destroy => true

    commenting it out fixed the issue.

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

Sidebar

Related Questions

I am creating a shared library using gcc and suspect that there may be
Note: I suspect my problem is related to this one: Visual Studio 2010 Publish
First off, I'm using Google AppEngine and Guice, but I suspect my problem is
I find this as a funny little problem. I think the reason lies behind
My company has a problem: we suspect that the NACHA files we are receiving
I suspect this applies to general ASP.Net too but I am not sure. If
One day I suspect I'll have to learn hadoop and transfer all this data
I am hoping that there is someone that can help me. I am using
I cannot get execv() to work in my C program. I suspect the problem
I've been struggling with this problem for 5 hours and I have a feeling

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.