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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T16:44:07+00:00 2026-05-14T16:44:07+00:00

Alright everyone this is a bit of a complicated setup so if I need

  • 0

Alright everyone this is a bit of a complicated setup so if I need to clarify the question just let me know.

I have a model:

class IconSet < ActiveRecord::Base
  has_many :icon_graphics
end

This Model has many icongraphics:

class IconGraphic < ActiveRecord::Base
  belongs_to :icon_set
  has_attached_file :icon
  has_attached_file :flagged
end

As you can see, IconGraphic has two attached files, basically two different versions of the icon that I want to load.

Now, this setup is working okay if I edit the icongraphic’s individually, however, for ease of use, I have all the icon graphics editable under the IconSet. When you edit the icon set the form loads a partial for the icongraphics:

<% form_for @icon_set, :html => {:class => 'nice', :multipart => true} do |f| %>
<fieldset>  
  <%= f.error_messages %>
  <p>
    <%= f.label :name %>
    <%= f.text_field :name, :class => "text_input" %>
  </p>
    <!-- Loaded Partial for icongraphics -->
    <div id="icon_graphics">
        <%= render :partial => 'icon_graphic', :collection => @icon_set.icon_graphics %>        
    </div>
    <div class="add_link">
    <%= link_to_function "Add an Icon" do |page|
        page.insert_html :bottom, :icon_graphics, :partial => 'icon_graphic', :object => IconGraphic.new
    end %>
    </div>
  <p><%= f.submit "Submit" %></p>
</fieldset>
<% end %>

This is based largely off of Ryan’s Complex Forms Railscast.

The partial loads the file_field forms:

<div class="icon_graphic">
<% fields_for "icon_set[icon_graphic_attributes][]", icon_graphic do |icon_form|-%>
    <%- if icon_graphic.new_record? -%>
        <strong>Upload Icon: </strong><%= icon_form.file_field :icon, :index => nil %><br/>
        <strong>Upload Flagged Icon: </strong><%= icon_form.file_field :flagged, :index => nil %>
        <%= link_to_function image_tag('remove_16.png'), "this.up('.icon_graphic').remove()"%><br/>
    <% else -%>
        <%= image_tag icon_graphic.icon.url %><br/>
        <strong>Replace <%= icon_graphic.icon_file_name %>: </strong><%= icon_form.file_field :icon, :index => nil %><br />
        <% if icon_graphic.flagged_file_name.blank? -%>
        <strong>Upload Flagged Icon: </strong><%= icon_form.file_field :flagged, :index => nil %>
        <% else -%>
        <strong>Replace <%= icon_graphic.flagged_file_name %>: </strong><%= icon_form.file_field :flagged, :index => nil %>
        <%= icon_form.hidden_field :flagged, :index => nil %>
        <% end -%>
        <%= link_to_function image_tag('remove_16.png'), "mark_for_destroy(this, '.icon_graphic')"%><br/>
        <%= icon_form.hidden_field :id, :index => nil %>
        <%= icon_form.hidden_field :icon, :index => nil %>
        <%= icon_form.hidden_field :should_destroy, :index => nil, :class => 'should_destroy' %>
        <br/><br/>
    <%- end -%>
<% end -%>
</div>

Now, this is looking fine when I add new icons, and fill both fields. However, if I edit the IconSet after the fact, and perhaps try to replace the icon with a new one, or if I uploaded only one of the set and try to add the second attachment, paperclip doesn’t put the attachments with the right IconGraphic Model.

It seems that even though I have the IconGraphic ID in each partial,

<%= icon_form.hidden_field :id, :index => nil %>

it seems that paperclip either creates a new IconGraphic or attaches it to the wrong one.

This all happens when you save the IconSet, which is setup to save the IconGraphic attributes.

I know this is complicated.. I may just have to go to editing each icon individually, but if anyone can help, I would appreciate it.

  • 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-14T16:44:08+00:00Added an answer on May 14, 2026 at 4:44 pm

    Alright I figured it out. The problem was that the id of each icon needs to come BEFORE the upload fields, not after:

    <% else -%>
          <!-- Moved the hidden fields before the upload fields -->
            <%= icon_form.hidden_field :id, :index => nil %>
            <%= icon_form.hidden_field :icon, :index => nil %>
            <%= icon_form.hidden_field :flagged, :index => nil unless icon_graphic.flagged_file_name.blank? %>
            <%= image_tag icon_graphic.icon.url %><%= image_tag icon_graphic.flagged.url unless icon_graphic.flagged_file_name.blank? %><br/>
            <strong>Replace <%= icon_graphic.icon_file_name %>: </strong><%= icon_form.file_field :icon, :index => nil %><br />
            <% if icon_graphic.flagged_file_name.blank? -%>
            <strong>Upload Flagged Icon: </strong><%= icon_form.file_field :flagged, :index => nil %>
            <% else -%>
            <strong>Replace <%= icon_graphic.flagged_file_name %>: </strong><%= icon_form.file_field :flagged, :index => nil %>
            <% end -%>
            <%= link_to_function image_tag('remove_16.png'), "mark_for_destroy(this, '.icon_graphic')"%><br/>
            <%= icon_form.hidden_field :should_destroy, :index => nil, :class => 'should_destroy' %>
            <br/><br/>
    <%- end -%>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Alright, I have a fairly simple design. class Update(models.Model): pub_date = models.DateField() title =
Alright. This may be difficult but I have been struggling for quite a bit
Alright everyone, is there a way to have 2 divs taking up 50% of
Alright so I have no idea how to even begin doing this But basically
I have just started to work with jquery so I need to make sure
Alright, I have a development branch that is tracked remotely and shared by everyone
Alright everyone, Let's say i wanted to make a system that used only assembly
Alright, so let's say I have a database with a table named COMPANY_PARAMETERS that
Alright, So I have an issue that is a bit weird. I am using
Alright, nub question. I know. Basic response might be Convert.ToInt32(string); But naturally, C# does

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.