【问题标题】:PDF upload Ruby on Rails server - DB PostgresPDF 上传 Ruby on Rails 服务器 - DB Postgres
【发布时间】:2015-11-26 03:28:39
【问题描述】:

您好,我正在尝试使用 Ruby Rails 将 PDF 文档存储在 PostgreSQL 数据库中。 目前我的代码如下所示:

数据库文件:

      '$20151126021922_create_pdf_creates.rb' 

class CreatePdfCreates < ActiveRecord::Migration
  def change
    create_table :pdf_creates do |t|
      t.binary :pdfload

      t.timestamps null: false
    end
  end
end

型号:

    '$pdf_create.rb'

class PdfCreate < ActiveRecord::Base    

end

控制器:

    '$pdf_creates_controller.rb'

class PdfCreatesController < ApplicationController
  before_action :set_pdf_create, only: [:show, :edit, :update, :destroy]

  # GET /pdf_creates
  # GET /pdf_creates.json
  def index
    @pdf_creates = PdfCreate.all
  end

  # GET /pdf_creates/1
  # GET /pdf_creates/1.json
  def show
  end

  # GET /pdf_creates/new
  def new
    @pdf_create = PdfCreate.new
  end

  # GET /pdf_creates/1/edit
  def edit
  end

  # POST /pdf_creates
  # POST /pdf_creates.json
  def newpdf
    @pdf_create = PdfCreate.new(pdf_create_params)
    respond_to do |format|
      if @pdf_create.save
        format.html { redirect_to @pdf_create, notice: 'Pdf create was successfully created.' }
        format.json { render :show, status: :created, location: @pdf_create }
      else
        format.html { render :new }
        format.json { render json: @pdf_create.errors, status: :unprocessable_entity }
      end
    end
  end

  def create
    @pdf_create = PdfCreate.new(pdf_create_params)
    #data = File.read(Rails.root + "tmp/consent(1).pdf")
    #Document.create pdfload: data 

    respond_to do |format|
      if @pdf_create.save
        format.html { redirect_to @pdf_create, notice: 'Pdf create was successfully created.' }
        format.json { render :show, status: :created, location: @pdf_create }
        format.pdf { send_data @pdf_create.render}

        else
           format.html { render :new }
        #  format.json { render json: @pdf_create.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /pdf_creates/1
  # PATCH/PUT /pdf_creates/1.json
  def update
    respond_to do |format|
      if @pdf_create.update(pdf_create_params)
        format.html { redirect_to @pdf_create, notice: 'Pdf create was successfully updated.' }
        format.json { render :show, status: :ok, location: @pdf_create }
      else
        format.html { render :edit }
        format.json { render json: @pdf_create.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /pdf_creates/1
  # DELETE /pdf_creates/1.json
  def destroy
    @pdf_create.destroy
    respond_to do |format|
      format.html { redirect_to pdf_creates_url, notice: 'Pdf create was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_pdf_create
      @pdf_create = PdfCreate.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def pdf_create_params
      params.require(:pdf_create).permit(:pdfload)
    end
end

我发送一个pdf表格,返回的结果是我:

{"id":5,"pdfload":null,"created_at":"2015-11-26T03:24:37.457Z","updated_at":"2015-11-26T03:24:37.457Z"}

怎么了?谢了

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 postgresql postgresql-9.1


    【解决方案1】:

    您发送的是整个新创建的记录,而不是存储在其中的 PDF 数据。

    尝试改变这个:

    format.pdf { send_data @pdf_create.render}
    

    到这里:

    format.pdf { send_data @pdf_create.pdfload}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-04
      • 2014-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-18
      • 2018-02-11
      • 2012-01-31
      相关资源
      最近更新 更多