如何通过 Paperclip rails 4 上传图片、word 文档和/或 PDF 文件 [英] How to upload image, word docs and/or PDF files via Paperclip rails 4

查看:57
本文介绍了如何通过 Paperclip rails 4 上传图片、word 文档和/或 PDF 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让用户将 Word 文档PDF 文件上传到我的 rails 应用程序.我的应用类似于 Pinterest 应用,用户可以创建图钉,在其中附加图片和说明(使用回形针将图像附加到图钉).

I would like to enable users to upload Word Docs and PDF files to my rails application. My app is similar to a Pinterest app, users can create Pins where they attach a picture followed by a description (used Paperclip to attach the image to the 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

我的引脚控制器:

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

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

I wonder if I just need to create another has_attached_file method for the Word docs and PDFs files within my Pin model and then create a view for users to upload the file.

推荐答案

这取决于...

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

If you want to attach an image AND a document you need to create another paperclip attribute for the document. On your model:

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) }

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

If you want to attach an image OR a document you can do the following:

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)}

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

If you choose the first option you will need two file inputs on your view, with the second only one. It's not right or wrong on this. It depends what you want to do.

这篇关于如何通过 Paperclip rails 4 上传图片、word 文档和/或 PDF 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆