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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T02:13:26+00:00 2026-06-06T02:13:26+00:00

I have a model Articles that has_many Assets which is a polymorphic model that

  • 0

I have a model “Articles” that has_many “Assets” which is a polymorphic model that I attach images to using Paperclip.
When I am editing an Article, I want to be able to delete the old image, and add a new one in the same stroke. I am using fields_for which seemed versatile enough since the Rails API says I can use it for a specific instance of Assets. So here is the relevant portion of my form:

Form:

=f.fields_for :assets do |ff|
  =ff.label "image"
  =ff.file_field :image

-unless @article.assets.first.image_file_name.nil?
  -@article.assets.each do |asset|
    =f.fields_for :assets, asset do |fff|
      =image_tag(asset.image.url(:normal))
      =fff.label "delete image"
      =fff.check_box :_destroy

The first fields_for is for adding images to articles, the second section is to delete assets that already exist. This form can add assets, delete assets, but it can’t do both at the same time.
That is the issue.
I suspect that the check_box is not directed enough or something.

Asset Model:

class Asset < ActiveRecord::Base
  belongs_to :imageable, :polymorphic => true

  has_attached_file :image, :styles => { :normal => "100%",:small => "100 x100>",:medium => "200x200>", :thumb => "50x50>" },
                        :storage => :s3, 
                        :s3_credentials => "#{Rails.root}/config/s3.yml", 
                        :path => "/:attachment/:id/:style/:filename"

Article controller /edit:

  def edit
    @article = Article.find(params[:id])
    @assets = @article.assets
    if @assets.empty?
      @article.assets.build
    end
  end

I look forward to seeing your responses.

  • 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-06T02:13:28+00:00Added an answer on June 6, 2026 at 2:13 am

    With my pitiful cries for help falling on deaf ears I was forced to set out on my own (probably for the best). I discovered the solution by fiddling with the logic of the form. Below is the set up that allows me to add a Paperclip attachment and delete one (or more) in one form submission:

    form:

    = form_for(@article, :action => 'update', :html => { :multipart => true}) do |f|
    .
    .
    .
      -@assets.each do |asset|
            =f.fields_for :assets, asset do |asset_fields|
              -if asset_fields.object.image_file_name.nil?
                =asset_fields.label "image"
                =asset_fields.file_field :image
              -else
                =image_tag(asset_fields.object.image.url(:normal))
                =asset_fields.check_box :_destroy
    

    My set up is: an article has_many assets which is a polymorphic model that holds image attachments for me.

    Research:

    http://apidock.com/rails/ActionView/Helpers/FormHelper/fields_for

    creating a form for deleting uploads that belongs to products

    -second link: provided the insight to use object method on the form helper supplied by fields_for, in my case it is asset_fields.object... which allowed me to mess with instances of @assets

    Here is the articles controller methods of interest:

      def edit
        @article = Article.find(params[:id])
        @assets = @article.assets
        @article.assets.build
      end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an article controller that deals with articles. I have a comment model
Just starting out here, I have a Article.rb model that stores articles. So it
I have model Article it has field title with some text that may contain
I have a controller called articles , which creates the articles model which gets
I have two model: User, Article A user can like or dislike many articles,
I have the following listview : http://www.vogella.de/articles/AndroidListView/article.html the chaper: 8. Tutorial: Domain Model and
I am using Ruby on Rails 3.0.7 and I have an User :has_many Articles
I have a user model that can follow some tags User has_many :tag_followings has_many
I have two models, Article and Post that both inherit from a base model
I have an Article model that has a foreign key relationship with Author that

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.