如何通过Paperclip rails 4上传图像,文档文档和/或PDF文件

Cyz*_*far 15 ruby-on-rails paperclip

我想让用户将Word文档PDF文件上传到我的rails应用程序.我的应用程序类似于Pinterest应用程序,用户可以创建Pins,其中附加图片后跟描述(使用Paperclip将图像附加到Pin).

这是我的Pins模型:

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
Run Code Online (Sandbox Code Playgroud)

我的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
Run Code Online (Sandbox Code Playgroud)

我想知道我是否只需要在我的Pin模型中has_attached_fileWord文档PDF文件创建另一种方法,然后为用户创建一个上传文件的视图.

Lea*_*xxx 35

这取决于...

如果要附加图像文档,则需要为文档创建另一个回形针属性.在你的模型上:

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) }
Run Code Online (Sandbox Code Playgroud)

如果要附加图像文档,可以执行以下操作:

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)}
Run Code Online (Sandbox Code Playgroud)

如果选择第一个选项,则视图中需要两个文件输入,第二个只有一个.这不是对或错.这取决于你想做什么.

  • 你运行`rails generate paperclip pin document`来创建迁移吗?此生成器将在Pin模型上创建document_file_name,document_file_size,document_content_type和document_updated_at属性 (2认同)
  • 更容易的是<%= link_to"我的文档",@ pin.document.url,target:"_ blank"%>并让您的浏览器处理pdf渲染.但我不知道这是否适用于所有浏览器和.doc文件.它适用于ubuntu,chrome和.pdf文件.另一种选择是创建一个控制器动作来下载文件.类似于/ pin /:id/download并使用http://apidock.com/rails/ActionController/Streaming/send_file. (2认同)