Mark O'Donnell
Mark O'Donnell

Reputation: 45

Show list of attachments using Paperclip in Rails Application

I am having issues with paperclip in my rails application. I am able to attach multiple files (PDFs) to my form, but when I try to show more than 1 attachment in the show.html.erb file I get errors.

The code that works in the edit and new views:

            <%= f.fields_for :assets do |asset| %>

               <% if asset.object.new_record? %>
                     <%= asset.file_field :document %>
               <% end %>

           <% end %>

    </div>

    <div class="existingPaperclipFiles">

         <% f.fields_for :assets do |asset| %>

              <% unless asset.object.new_record? %>

                    <div class="thumbnail">
                            <%= link_to( image_tag(asset.object.document.url(:thumb)), asset.object.document.url(:original) ) %>
                            <%= asset.check_box :_destroy %>
                    </div>

              <% end %>        

         <% end %>
    </div>

I have created a separate assets model to keep all the attachments related to my equipment model. When I create a "link_to asset.object.document.url" in the show view I get NoMethod errors. I want to attach both .doc, PDF, and image files to my application if there is a better way than paperclip please help!

The assets model:

class Asset < ActiveRecord::Base
  belongs_to :equipment

  has_attached_file :document, :styles => {:thumb => '150x150#', :medium => '300x300#', :large => '600x600#' }

end

The equipment model:

class Equipment < ActiveRecord::Base
  validates :equipment_id, presence: true
  validates :location, presence: true

  has_many :assets, :dependent => :destroy
  accepts_nested_attributes_for :assets, :allow_destroy => true

end

My equipment_controller:

class EquipmentController < ApplicationController

  def index
    @equipment = Equipment.paginate(page: params[:page])
  end

  def show
    @equipment = Equipment.find(params[:id])


    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @equipment }
    end
  end

  def new
    @equipment = Equipment.new
    5.times {@equipment.assets.build}
  end

  def create
    @equipment = Equipment.new(params[:equipment])

    respond_to do |format|
      if @equipment.save
        format.html { redirect_to @equipment, notice: 'Equipment was successfully added.' }
        format.json { render json: @equipment, status: :created, location: @equipment }
      else
        format.html { render action: "new" }
        format.json { render json: @equipment.errors, status: :unprocessable_entity }
      end
    end
  end

  def edit
    @equipment = Equipment.find(params[:id])
    5.times {@equipment.assets.build}
  end

  def update
    @equipment = Equipment.find(params[:id])

    respond_to do |format|
      if @equipment.update_attributes(params[:equipment])
        format.html { redirect_to @equipment, notice: 'Equipment was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @equipment.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @equipment = Equipment.find(params[:id])
    @equipment.destroy

    respond_to do |format|
      format.html { redirect_to equipment_url }
      format.json { head :no_content }
    end
  end

  private

    def correct_user
      @Equipment = Equipment.find(params[:id])
      redirect_to(root_path) unless current_user?(@Equipment)
    end

    def admin_user
      redirect_to(root_path) unless current_user.admin?
    end
end

Any help is greatly appreciated.

Upvotes: 1

Views: 2743

Answers (2)

Mark O&#39;Donnell
Mark O&#39;Donnell

Reputation: 45

I actually was able to figure this out by doing the following:

  <% for asset in @equipment.assets %>
    <p>
    <%= link_to asset.document_file_name,
                          asset.document.url(:original) %>
    </p>
  <% end %>

Upvotes: 1

Eric K
Eric K

Reputation: 395

You didn't give the code for the show view where you're having the error, but you should be able to do something like this to show thumbnails with links to the original files:

<% @equipment.assets.each do |asset| %>

    <%= link_to image_tag(asset.document.url(:thumb)), asset.document.url(:original) %>

<% end %>

I'm not sure why you have "object" in your Paperclip URLs, as I haven't seen that format used for Paperclip.

Upvotes: 2

Related Questions