Ruby on rails 如何通过曲别针rails 4上传图像、word文档和/或PDF文件

Ruby on rails 如何通过曲别针rails 4上传图像、word文档和/或PDF文件,ruby-on-rails,paperclip,Ruby On Rails,Paperclip,我希望用户能够将Word文档和PDF文件上传到我的rails应用程序中。我的应用程序类似于Pinterest应用程序,用户可以创建Pin,然后在其中附加图片和描述(使用回形针将图像附加到Pin) 这是我的引脚型号: class Pin < ActiveRecord::Base belongs_to :user has_attached_file :image, :styles => { :medium => "300x300>", :thumb =>

我希望用户能够将Word文档PDF文件上传到我的rails应用程序中。我的应用程序类似于Pinterest应用程序,用户可以创建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
class Pin{:medium=>“300x300>”,:thumb=>“100x100>”}
验证附件:图像,内容类型:{content类型:[“image/jpg”,“image/jpeg”,“image/png”,“image/gif”]}
验证:映像,状态:true
结束
My引脚控制器:

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
class PinsControllerparams[:page],:per\u page=>15)
结束
def秀
结束
def新
@pin=当前_user.pins.build
结束
定义编辑
结束
def创建
@pin=当前用户.pins.build(pin参数)
如果@pin.save
将_重定向到@pin,注意:“pin已成功创建。”
其他的
渲染操作:“新建”
结束
结束
def更新
如果@pin.update(pin_参数)
将_重定向到@pin,注意:“pin已成功更新。”
其他的
渲染操作:“编辑”
结束
结束
def销毁
@销
重定向到pins\u url
结束
私有的
def设置针
@pin=pin.find(参数[:id])
结束
def正确的用户
@pin=当前用户.pins.find_by(id:params[:id])
重定向到Pin路径,注意:如果@Pin.nil,则“无权编辑此Pin”?
结束
def引脚参数
参数require(:pin).permit(:description,:image)
结束
结束
我想知道我是否需要为我的Pin模型中的Word文档和PDF文件创建另一个
has_attached_file
方法,然后创建一个视图供用户上传文件。

这取决于

如果要附加图像和文档,则需要为文档创建另一个曲别针属性。在您的模型上:

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/文档和图像)。我为
:文档创建了一个新输入,如下所示:

。我试过了,它给了我一个错误
未定义的方法文档\u content\u type'
。我的pin控制器出现问题:
def create@pin=current_user.Pins.build(pin_参数)如果@pin.save重定向到@pin,请注意:“pin已成功创建”。其他渲染操作:“new”end
。是否运行了
rails生成回形针文档来创建迁移?此生成器将在您的Pin模型上创建文档文件名、文档文件大小、文档内容类型和文档更新属性。记住:运行“rails生成迁移将文件添加到Pin”,然后
rake db:migrate
。现在一切正常。我必须考虑MVC!现在,对于其他用户查看和/或下载pdf/文档,我应该创建一个与我的图像类似的视图吗?如果是这样的话,我将不得不深入研究这个问题,并试着自己解决。更简单的方法是让你的浏览器处理pdf渲染。但我不知道这是否适用于所有浏览器和.doc文件。它适用于我的ubuntu、chrome和.pdf文件。另一个选项是创建控制器操作以下载文件。类似于/pin/:id/下载并使用。