В моем (очень первом) приложении rails у меня есть модель под названием «Аннотации»; это потребует некоторых общих данных, а также вложения (PDF). Затем мне нужно сделать фактические аннотации к этому вложению/PDF («аннотировать») и сохранить результаты в поле в модели «Аннотации» (как JSON?).Rails - аннотировать PDF в браузере
В настоящее время, я думаю, я должен создать новый метод «аннотировать» в AnnotationsController (необходимо обновить объект аннотации) и вызвать новое представление под названием «annotate.html.erb
».
Любой совет, как идти о «пути рельсов»?
Update тем временем, у меня есть:
модель (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"
Contr Oller (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
- так выглядит теперь проблема маршрутизации (для). (Маршрутизация по-прежнему является немного загадкой для меня :-))
Сделав это, следуйте руководству по началу работы, чтобы сделать это. Затем, после создания аннотации, необходимо добавить эту функцию, чтобы фактически аннотировать PDF. Что, на ваш взгляд, делает set_annotation? Это то же самое, что и шоу - верно? И почему это частный метод? Выполнение этого результата при ошибке маршрутизации - нет маршрутов совпадений [post] "/ аннотации/7" –
Для того, чтобы оставить комментарий, вам нужно будет показать нам, что вы сделали до сих пор. Некоторый код поможет. –
Кристиан - добавьте обновление. Хорошо оценено. –