Как получить данные из кэша фотографий с помощью shrine - PullRequest
0 голосов
/ 18 июня 2020
• 1000

ActionController::RoutingError (No route matches [GET] "/uploads/cache/47eee6dc368f20db1e26fa5176552136.jpg"):

Я думаю, что это ошибка Я не могу отправить правильный путь, я не решаю эту ошибку,

кто-то мне помогает, пожалуйста?

Часть заявки. js:

var uppy = Uppy.Core({
   autoProceed: true,
   restrictions: {
     allowedFileTypes: fileInput.accept.split(','),
   }
  })
  .use(Uppy.FileInput, {
   target: formGroup,
   locale: { strings: { chooseFiles: 'Choose file' } },
   })
 .use(Uppy.Informer, {
   target: formGroup,
   })
  .use(Uppy.ProgressBar, {
   target: imagePreview.parentNode,
   })
  .use(Uppy.ThumbnailGenerator, {
    thumbnailWidth: 600,
   })
  .use(Uppy.XHRUpload, {
    endpoint: '/app/assets/images/uploads',
   })

  uppy.on('upload-success', (file, response) => {

  var uploadedFileData = JSON.stringify(response.body['data'])
  // set hidden field value to the uploaded file data so that it's submitted with the form as the attachment
console.log(response.uploadURL)

cropbox(imagePreview, response.uploadURL, {
  onCrop(detail) {
    let fileData = JSON.parse(uploadedFileData)
    fileData['metadata']['crop'] = detail
    hiddenInput.value = JSON.stringify(fileData)
  }
})
})

uppy.on('thumbnail:generated', (file, preview) => {
  imagePreview.src = preview
})
}

форма. html .erb:

 <div class="form-group">
  <%= f.fields_for :photos, @blog_form.get_photos do | photos_fields| %>

    <%= photos_fields.label :image %>

    <%= photos_fields.hidden_field :image, value: photos_fields.object.cached_image_data, class: "upload-data" %>

    <%= photos_fields.file_field :image, accept: ImageUploader::ALLOWED_TYPES.join(","),
  data: {
    upload_server: "upload_server",
    preview_element: "preview-photo",
    upload_result_element: "preview-photo-upload-result",
  }%>

    <div class="image-preview">
   <!-- static link to the thumbnail generated on attachment -->
   <%= image_tag photos_fields.object.image_url(:medium),
    width: 300,
    class: "img-thumbnail file-upload-preview",
    id: "preview-photo" %>
 </div>


   <% end %>

routes.rb:

 Rails.application.routes.draw do

 mount ImageUploader.derivation_endpoint => "/derivations/image"
 mount ImageUploader.upload_endpoint(:cache) => "/app/assets/images/uploads"
 get "/derivations/image/*rest" => "derivations#image"


 get 'sessions/new'

 devise_for :users

 resources :users do

  resources :blogs

 end
 # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end

Плагин Shrine.rb:

require "shrine"
require "shrine/storage/file_system"




Shrine.storages = {
 cache: Shrine::Storage::FileSystem.new("app/assets/images", prefix: "uploads/cache"), # temporary
 store: Shrine::Storage::FileSystem.new("app/assets/images", prefix: "uploads"),       # permanent
}


Shrine.plugin :pretty_location
Shrine.plugin :determine_mime_type, analyzer: :marcel


Shrine.plugin :activerecord           # loads Active Record integration
Shrine.plugin :cached_attachment_data # for retaining the cached file across form redisplays
Shrine.plugin :restore_cached_data # re-extract metadata when attaching a cached file

Shrine.plugin :validation_helpers


Shrine.plugin :derivatives
Shrine.plugin :derivation_endpoint, secret_key: "secret", expires_in: 10

Shrine.plugin :upload_endpoint, url: true
Shrine.plugin :default_url


 Shrine.plugin :backgrounding

Плагин Image_uploader.rb:

plugin :derivatives
plugin :upload_endpoint
plugin :derivation_endpoint, prefix: "/app/assets/images/uploads"

Я проверил путь с console.log, так что response.uploadURL

/uploads/cache/5a01d66280a502d7256acffeb1b21bca.jpeg
...