Rails - 在浏览器中注释PDF

时间:2016-09-07 07:33:07

标签: ruby-on-rails pdf annotations

在我的(第一个)rails应用程序中,我有一个名为" Annotations&#34 ;;的模型。这将采用一些通用数据以及附件(PDF)。接下来,我需要能够对此附件/ PDF进行实际注释("注释")并将结果存储在" Annotations"上的字段上。 model(作为JSON?)。

目前,我认为我应该创建一种新方法"注释"在AnnotationsController中(需要更新注释对象)并调用名为" annotate.html.erb"的新视图。

任何建议如何进行"轨道方式" ?

更新 同时,我有:

模型annotation.rb

class Annotation < ApplicationRecord
    has_many :comments, dependent: :destroy
    belongs_to :documenttype
    has_attached_file :file, styles: { large: "600x600>", medium: "500x500>", thumb: "150x150#" }, default_url: "/images/:style/missing.png"

    accepts_nested_attributes_for :documenttype

    validates_attachment_content_type :file, content_type: ['image/jpeg', 'image/png', 'image/gif', 'application/pdf']

    validates :name, presence: true, uniqueness: true, length: { minimum: 10, maximum: 50 }
    validates :description, length: { minimum: 20, maximum: 500 }
    validates :documenttype, presence: true
    validates :file, presence: true
end

路由

  Rails.application.routes.draw do
      root 'dashboard#index'
      devise_for :users
      resources :users,:documenttypes, :documents
      resources :annotations do
        resources :comments
  end
  get "annotate", to: "annotations#annotate"

控制器(AnnotationsController)

class AnnotationsController < ApplicationController
before_action :annotate, only: [:edit, :update ]

def index
    @annotations = Annotation.all
end

def show
    @annotation = Annotation.find(params[:id])
end

def new
    @annotation = Annotation.new
end

def edit
    @annotation = Annotation.find(params[:id])
end

def create
    @annotation = Annotation.new(annotation_params)

    if @annotation.save
        redirect_to @annotation
    else
        render 'new'
    end
end

def update
    @annotation = Annotation.find(params[:id])

    if @annotation.update(annotation_params)
        redirect_to @annotation
    else
        render 'edit'
    end
end

def destroy
    @annotation = Annotation.find(params[:id])
    @annotation.destroy

    redirect_to annotations_path
end

private

    def annotate
        @annotation = Annotation.find(params[:id])
    end

    def annotation_params
        params.require(:annotation).permit(:name, :description, :file, :active, :documenttype_id)
    end
end
呈现1个表单的

视图(使用simple_form)

    <div class="container-fluid">
     <div class="row">
    <h4>Annotation</h4>
    <div class="col-md-6">
      <%= simple_form_for @annotation,  html: { class: 'form-horizontal', multipart: true },
        wrapper: :horizontal_form,
        wrapper_mappings: {
            check_boxes: :horizontal_radio_and_checkboxes,
            radio_buttons: :horizontal_radio_and_checkboxes,
            file: :horizontal_file_input,
            boolean: :horizontal_boolean
          } do |f| %>

          <%= f.error_notification %>

          <% if @annotation.file.blank? %>
            <%= f.input :file, as: :file, input_html: { accept: ('application/pdf') } %>
            <% else %>
          <% end -%>

            <%= f.input :name, placeholder: 'Enter name' %>

            <%= f.input :description, placeholder: 'Description' %>

            <%= f.association :documenttype %>

          <%= f.input :active, as: :boolean %>

          <%= f.button :submit %>
          <% unless @annotation.file.blank? %>
          <%= link_to ' Annotate', annotate_path(@annotation), :class => "btn btn-default" %>
          <% end -%>

      <% end -%>

      <p><br><%= link_to 'List' , annotations_path %></p>

    </div>

    <% unless @annotation.file.blank? %>
    <div class="col-md-6">
      <p><strong>File name: </strong><%= @annotation.file_file_name %></p>
      <iframe src="<%= @annotation.file %>" width=100% height=450px class="img-rounded"></iframe>
    </div>
    <% end %>
  </div>

  <% unless @annotation.new_record? %>
  <div class="row">
    <hr>
      <div class="col-md-6">
        <%= render @annotation.comments %>
      </div>

      <div class="col-md-6">
        <%= render 'comments/form' %>
      </div>

  </div>
  <% end -%>

</div>

另外,我创建了一个视图调用annotate.html.erb

现在称为http://localhost:3000/annotate下的静态页面;虽然我认为它应该在http://localhost:3000/annotations/annotate/:id之下 - 所以现在看起来像一个路由问题。 (路由对我来说仍然有点神秘:-))

2 个答案:

答案 0 :(得分:0)

如果您想以rails方式执行此操作:

您的模型应该是单数,所以:Annotation.rb

class Annotation < ApplicationRecord

end

您的控制器应调用AnnotationsController并使用标准CRUD方法:newcreate(以创建新注释)。 editupdate更新注释,destroy删除注释。

有点像:

class AnnotationsController < ApplicationController
  before_action :set_annotation, only: [:edit, :update, :destroy]

  def index
    @annotations = Annotation.all
  end

  def new
    @annotation = Annotation.new
  end

  def create
    @annotation = Annotation.new(annotation_params)

    if @annotation.save
      redirect_to annotations_path, notice: "annotations created"
    else
      render action: 'new'
    end
  end

  def edit
  end

  def destroy
    @annotation.destroy
    redirect_to annotations_path
  end

  def update
    if @annotation.update(annotation_params)
      redirect_to annotations_path
    else
      render action: 'edit'
    end
  end

  private

  def set_annotation
    @annotation = Annotation.find(params[:id])
  end

  def annotation_params
    params.require(:annotation).permit(:name, ...)
  end
end

您的观点应该位于views/annotations/

由于这是您的第一个rails应用程序,我建议您使用scaffolding选项来构建您的应用程序,根据文档是:

  

Rails中的脚手架是一整套模型,数据库迁移   该模型,操纵它的控制器,查看和操纵视图   数据,以及上述各项的测试套件。

rails g scaffold Annotate

请参阅:http://guides.rubyonrails.org/command_line.html

答案 1 :(得分:0)

找到路由get "annotations/:id/annotate" => "annotations#annotate", as: 'annotate'

set_annotation方法不应该是私有的。

可能有更好的路由(总是欢迎),但现在可以使用。