如何通过Paperclip rails 4上传图片、Word文档和/或PDF文件

17
我希望用户能够上传Word文档和PDF文件到我的Rails应用程序中。我的应用程序类似于Pinterest应用程序,用户可以创建“Pin”,并在其上附加图片和描述(使用Paperclip将图像附加到“Pin”)。以下是我的“Pin”模型:
class Pin < ActiveRecord::Base
    belongs_to :user
    has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }
    validates_attachment :image, content_type: { content_type: ["image/jpg", "image/jpeg", "image/png", "image/gif"] }
    validates :image, presence: true

    end

我的Pins控制器:

class PinsController < ApplicationController
  before_action :set_pin, only: [:show, :edit, :update, :destroy]
  before_action :correct_user, only: [:edit, :update, :destroy]
  before_action :authenticate_user!, except: [:index, :show]

  def index
    @pins = Pin.all.order("created_at DESC").paginate(:page => params[:page], :per_page => 15)
  end

  def show
  end

  def new
    @pin = current_user.pins.build
  end

  def edit
  end

 def create
    @pin = current_user.pins.build(pin_params)
    if @pin.save
      redirect_to @pin, notice: 'Pin was successfully created.'
    else
      render action: 'new'
    end
  end

  def update
    if @pin.update(pin_params)
      redirect_to @pin, notice: 'Pin was successfully updated.'
    else
      render action: 'edit'
    end
  end

  def destroy
    @pin.destroy
    redirect_to pins_url
  end

  private

    def set_pin
      @pin = Pin.find(params[:id])
    end

    def correct_user
      @pin = current_user.pins.find_by(id: params[:id] )
      redirect_to pins_path, notice: "Not authorized to edit this Pin" if @pin.nil?
    end


    def pin_params
      params.require(:pin).permit(:description, :image)
    end
end

我在想是否只需在我的 Pin 模型中为 Word文档 PDF 文件创建另一个 has_attached_file 方法,然后创建一个视图供用户上传文件。

1个回答

37

这要看情况而定...

如果您想同时附上一张图片一个文档,您需要为文档创建另一个纸夹属性。在您的模型中:

has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }
validates_attachment :image, content_type: { content_type: ["image/jpg", "image/jpeg", "image/png", "image/gif"] }

has_attached_file :document
validates_attachment :document, :content_type => { :content_type => %w(application/pdf application/msword application/vnd.openxmlformats-officedocument.wordprocessingml.document) }

如果您想要附加一张图片或者一个文档,您可以按照以下步骤进行:

has_attached_file :document
validates_attachment :document, :content_type => {:content_type => %w(image/jpeg image/jpg image/png application/pdf application/msword application/vnd.openxmlformats-officedocument.wordprocessingml.document)}

如果您选择第一种选项,则需要在视图中使用两个文件输入,而第二个选项只需要一个。这并不是对错之分,而是取决于您想要做什么。


我正在使用第一种选项(上传PDF /文档和图像)。 我为:document创建了一个新的输入,如下所示:<div class="form-group"> <%= f.label :document %><br> <%= f.file_field :document, class: "form-control" %> </div>。 我尝试了它,并且它给了我一个错误undefined method document_content_type'。 我的Pins控制器出了点问题:def create @pin = current_user.pins.build(pin_params) if @pin.save redirect_to @pin, notice: 'Pin was successfully created.' else render action: 'new' end end - Cyzanfar
2
你运行了 rails generate paperclip pin document 来创建迁移吗?这个生成器将在你的 Pin 模型上创建 document_file_name、document_file_size、document_content_type 和 document_updated_at 属性。 - Leantraxxx
刚想起来要做这个:运行 rails generate migration add_file_to_pin 然后 rake db:migrate。现在可以正常工作了。我必须考虑 MVC - Cyzanfar
现在,为了让其他用户查看和/或下载PDF /文档,我只需创建一个类似于我用于我的图像的视图 <%= image_tag @pin.image.url(:medium) % > 吗?还是比这更复杂?如果是这样,我就必须深入研究并尝试自己解决问题。 - Cyzanfar
2
更简单的方法是 <%= link_to "我的文档", @pin.document.url, target: "_blank" %>,让您的浏览器处理PDF渲染。但我不知道这是否适用于所有浏览器和.doc文件。在我的Ubuntu、Chrome和.pdf文件中可以使用。另一个选项是创建一个控制器操作来下载文件。类似于 /pin/:id/download 并使用 http://apidock.com/rails/ActionController/Streaming/send_file。 - Leantraxxx
@Leantraxxx 谢谢,你让我的一天变得美好了。使用 Paperclip 验证图像或文档真的很难找到方法。 - SSR

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接