神社衍生插件方法:image_derivatives!数组

谢谢您的帮助!

我正在尝试为在Rails控制器操作中上载的图像创建派生类。

首先,我用自制软件安装了imagemagickvips

brew install imagemagick
brew install vips

我的Gemfile中包含以下内容:

gem 'shrine','~> 3.0'
gem 'image_processing','~> 1.8'
gem "aws-sdk-s3","~> 1.14"

app/uploaders/image_uploader.rb中,我有:

require "image_processing/mini_magick"
 
class ImageUploader < Shrine

    plugin :cached_attachment_data
    plugin :determine_mime_type
    # plugin :remove_attachment
    plugin :validation
    plugin :validation_helpers
    plugin :derivatives

    Attacher.validate do
        validate_max_size 100.megabytes,message: 'Attachment is too large'
        validate_mime_type_inclusion [ 'image/jpg','image/jpeg','image/png','image/gif' ]
    end

    Attacher.derivatives_processor do |original|
        magick = ImageProcessing::MiniMagick.source(original)

        {
            large:  magick.resize_to_limit!(800,800),medium: magick.resize_to_limit!(500,500),small:  magick.resize_to_limit!(300,300),}
    end

end

app/config/initializers/shrine.rb中,我有:

require 'shrine'
require 'shrine/storage/s3'

Shrine.plugin :activerecord

s3_options = {
    bucket: Figaro.env.S3_BUCKET,region: Figaro.env.S3_REGION,access_key_id: Figaro.env.S3_KEY,secret_access_key: Figaro.env.S3_SECRET
}

Shrine.storages = {
    cache: Shrine::Storage::S3.new(prefix: "cache",**s3_options),store: Shrine::Storage::S3.new(s3_options)
}

在我的控制器中,我有:

  def create
    handle_money
    @product = Product.new(product_params)
    respond_to do |format|

      if @product.save
        @product.photo.image_derivatives!
        format.html { redirect_to purchases_products_path,notice: 'Product was successfully created.' }
      else
        @photo = @product.build_photo
        format.html { render :new }
        format.json { render json: @product.errors,status: :unprocessable_entity }
      end
    end
  end

我的模型包括Shrine图片附件并设置关联:

class Photo < ApplicationRecord
    include ImageUploader::Attachment(:image)
    belongs_to :product
end
class Product < ApplicationRecord
    monetize :price_cents
    has_many :orders,dependent: :destroy
    has_one :photo,dependent: :destroy

    accepts_nested_attributes_for :photo
end

我的Rails表单是:

<%= form_for product,multipart: true do |f| %>
  <% if f.errors.any? %>
    <%= render partial: 'shared/flash_message',locals: { alert_type: 'danger',msg: f.object.errors.full_messages.to_sentence } %>
  <% end %>

  <div class="form-group">
    <%= f.label :name %>
    <%= f.text_field :name,value: f.object.name&.humanize || nil,required: true,class: 'form-control' %>
  </div>

  <div class="form-group">
    <%= f.label :price %>
    <%= f.text_field :price,value: f.object.price || nil,class: 'form-control' %>
  </div>

  <%= f.fields_for :photo do |photo| %>
    <div class='form-group'>
      <%= photo.hidden_field :image,value: photo.object.cached_image_data %>
      <div class='input-group'>
        <div class='custom-file'>
          <%= photo.file_field :image,class: 'custom-file-input' %>
          <label class='custom-file-label' for='inputGroupFile04'>Choose File</label>
        </div>
      </div>
    </div>
  <% end %>

  <div class="actions d-flex align-items-baseline justify-content-between">
    <%= link_to "Back",purchases_products_path(),class: "btn btn-secondary p-2" %>
    <%= form.submit "Submit",class: "btn btn-primary p-2" %>
  </div>
<% end %>

我的错误发生在控制器中的行:

@product.photo.image_derivatives!

,日志显示以下内容:

NoMethodError - undefined method `bytesize' for #<Array:0x00007ffc826ce518>:
  app/controllers/products_controller.rb:37:in `block in create'
  app/controllers/products_controller.rb:32:in `create'

该错误可能是imagemagick和vips库的结果,但是我不确定如何进一步跟踪该错误。

无需使用衍生插件(以及与之相关的后续代码)即可进行上传。

我想清楚地知道此错误的解决方案,以及追踪这些错误的更好方法(在gem'pry'之外)。再次感谢您的帮助!

iCMS 回答:神社衍生插件方法:image_derivatives!数组

那对我有帮助:

https://github.com/shrinerb/shrine/issues/492

我已经直接从github存储库中使用最新版本。

删除当前版本的Shrine并将此行添加到Gemfile中:

gem 'shrine',github: 'shrinerb/shrine'

本文链接:https://www.f2er.com/1811384.html

大家都在问