我厌倦了将不同大小的图像上传到AWS S3,但没有显示任何内容。这是我的配置:
class AttachmentUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
include CarrierWave::MiniMagick
# Choose what kind of storage to use for this uploader:
storage :fog
# storage :fog
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
def fog_public
true
end
# Create different versions of your uploaded files:
version :large do
process resize_to_fit: [800, 800]
end
version :medium, from_version: :large do
process resize_to_limit: [500, 500]
end
version :thumb, from_version: :medium do
process resize_to_fit: [100, 100]
end
version :square do
process resize_to_fill: [500,500]
end
end
以下是我的模特:
class Product < ActiveRecord::Base
mount_uploaders :attachments, AttachmentUploader
belongs_to :user
has_one :product_category
validates :name, :presence => true
validates :product_category_id, :presence => true
validates :payment_type, :presence => true
end
我的控制员:
class ProductsController < ApplicationController
def layout_by_resource
"product"
end
def index
@products = Product.all
end
def new
@product = Product.new
@categories = ProductCategory.all
end
def create
@product = Product.new(product_params)
@product.payment_type = params[:product][:payment_type].to_i
respond_to do |format|
if @product.save
# format.html { redirect_to @product, notice: 'product was successfully created.' }
format.html { redirect_to products_path, notice: 'Product was successfully created.' }
format.json { render :show, status: :created, location: @product }
else
# format.html { render :new }
format.html { redirect_to products_path }
format.json { render json: @product.errors, status: :unprocessable_entity }
end
end
end
private
def set_product
@product = Product.find(params[:id])
end
def product_params
params.require(:product).permit(:name, :product_category_id, {attachments: []})
end
end
当我在attactment_uploader.rb
中注释掉创建不同的版本时,我可以将一个文件上传到S3。谢谢!!
答案 0 :(得分:0)
请注意,只有较大的图像可以调整大小,但小图像无法调整大小。
试试这个。
class AttachmentUploader < CarrierWave::Uploader::Base
# Include RMagick or MiniMagick support:
# include CarrierWave::RMagick
include CarrierWave::MiniMagick
# Choose what kind of storage to use for this uploader:
storage :fog
# storage :fog
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
def fog_public
true
end
# Create different versions of your uploaded files:
version :standard do
process :resize_to_fill => [344, 258, :north]
end
version :large do
process :resize_to_fit => [800,800]
end
version :medium do
process :resize_to_fill => [400,400]
end
version :thumb do
process :scale => [100, 100]
end
version :square do
process resize_to_fill: [500,500]
end
end