在Rails应用程序中使用Paperclip显示附件列表

时间:2012-03-27 19:34:16

标签: ruby-on-rails ruby paperclip

我在rails应用程序中遇到了回形针问题。我能够将多个文件(PDF)附加到我的表单,但是当我尝试在show.html.erb文件中显示多个附件时,我会收到错误。

在编辑和新视图中使用的代码:         

            <%= 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>

我创建了一个单独的资产模型,以保留与我的设备模型相关的所有附件。当我在show视图中创建“link_to asset.object.document.url”时,我得到NoM​​ethod错误。我想将.doc,PDF和图像文件附加到我的应用程序中,如果有比paperclip更好的方法请帮助!

资产模型:

class Asset < ActiveRecord::Base
  belongs_to :equipment

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

end

设备型号:

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

我的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

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

您没有为显示视图的代码提供错误,但您应该可以执行此类操作以显示包含原始文件链接的缩略图:

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

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

<% end %>

我不确定为什么你的Paperclip网址中有“对象”,因为我还没有看到用于Paperclip的格式。

答案 1 :(得分:0)

我实际上能够通过以下方式解决这个问题:

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