Search code examples
ruby-on-railsrubyruby-on-rails-3paperclipjquery-file-upload

unable to see the attributes in has_many images after multiple file uploads


I have a has_many images association where i try to upload multiple images in a form but i am unable to see the associated attributes when i click the submit button.Its a simple association of bug has_many images and images belongs_to bug.I am using jQuery-File-Upload for file upload.The association is as shown :-

my first model

    class Bug < ActiveRecord::Base    
    has_many :bug_images, :dependent => :destroy
    accepts_nested_attributes_for :bug_images, :allow_destroy => true
    attr_accessible :bug_images_attributes

    has_attached_file :bug_image,
                    :styles => { :thumb => "75x75>", :small => "150x150>" },
                    :url => '/:class/:id/:attachment?style=:style'
end

my second model

  class BugImage < ActiveRecord::Base

   belongs_to :bug

    def image_file=(input_data)
    self.name = input_data.original_filename
    self.image_type = input_data.content_type.chomp
    self.size = File.new(input_data).size
   end

end

my table definition

    class CreateBugImages < ActiveRecord::Migration
  def self.up
    create_table :bug_images do |t|
      t.string :name
      t.references :bug 
      t.string :image_type
      t.integer :image_size

      t.timestamps
    end
  end

  def self.down
    drop_table :bug_images
  end
end

##my view page where i can upload multiple file uplaod but on submit unable to get the uploaded images

  <% form_for(@bug, :url => "/myapp/bugs/create",:html=>{:multipart => true,:id=>"form_bug"}) do |f| %>
         <div class="row-fluid">
        <label class="FormLabel">Severity <font color="red">*</font></label> 
           %= f.select :sevearity,Bug::Severity_Type ,{:prompt => "Select    Severity                  Level"},:class=>'selectpicker'} %>       </div>        
        <div class="row-fluid">
        <label class="FormLabel">Attribute<font color="red">*</font></label>
        <%= select_tag 'attribute_id', options_for_select(@attributes.collect{ |u| [u.attribute_name,  u.id]}.insert(0, "")), :class=>'chzn-select',:required=>:true %>
       </div>

<!--  here i upload multiple images for a bug ->       
         <% f.fields_for :bug_images do |builder| %>
            <%= builder.file_field :image_file %>
          <% end %>
          <%end%>

my server log where i cant see the bug_images_attributes

    "---------in create--------------"
"bug"=>{"sevearity"=>"S1"}, "attribute_id"=>"7", "controller"=>"bugs", "action"=>"create", "HTTP_START_TIME"=>20
13-10-08 18:56:01 +0530}
">>>>>>>>>>>>>>>> application_api"

Solution

  • if you want to attach multiple images to a bug, the bug_image model should have the has_attached_file declaration.

    class Bug
      has_many :bug_images
    end
    
    class BugImage
      belongs_to :bug
      has_attached_file :bug_image
    end