神社衍生品插件方法: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

我的模型包括神社图像附件和设置关联:

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, required: true, 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'

错误可能是imagemagik和vips库造成的,但我不确定如何进一步跟踪错误。

上传不需要使用派生插件(以及后续的相关代码(。

我想知道这个错误的解决方案(清楚地(,以及追踪这些错误的更好方法(在宝石"撬"之外(。再次感谢您的帮助!

这对我有帮助:

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

我直接使用了github repo的最后一个版本。

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

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

最新更新