carrierwaveuploader/carrierwave
Rspec using CarrierWave for uploading multiple images-Qiita
[Rails] Introduction of carrierwave and MiniMagick when you want to save multiple images --Qiita
gem 'carrierwave', '~> 2.0'
Create uploader after bundle install
be rails g uploader Images
app / uploaders / images_uploader.rb is created.
contents
class ImagesUploader < 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 :file
# storage :fog
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
# Provide a default URL as a default if there hasn't been a file uploaded:
# def default_url(*args)
# # For Rails 3.1+ asset pipeline compatibility:
# # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png "].compact.join('_'))
#
# "/images/fallback/" + [version_name, "default.png "].compact.join('_')
# end
# Process files as they are uploaded:
# process scale: [200, 300]
#
# def scale(width, height)
# # do something
# end
# Create different versions of your uploaded files:
# version :thumb do
# process resize_to_fit: [50, 50]
# end
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
# def extension_whitelist
# %w(jpg jpeg gif png)
# end
# Override the filename of the uploaded files:
# Avoid using model.id or version_name here, see uploader/store.rb for details.
# def filename
# "something.jpg " if original_filename
# end
end
Added images column to Image table. (Already linked with the Post table when creating the Image table)
When uploading multiple files, it seems to be saved in json format, so specify it.
be rails g migration add_images_to_image images:json
migrate contents
class AddImagesToImage < ActiveRecord::Migration[6.0]
def change
add_column :images, :images, :json
end
end
Rails db: migrate to add columns
models/image.rb
class Image < ApplicationRecord
belongs_to :post
validates :images, presence: true
mount_uploaders :images, ImagesUploader
end
spec/factories/images.rb
FactoryBot.define do
factory :image do
images { [ Rack::Test::UploadedFile.new(Rails.root.join('spec/fixtures/test.jpg'), 'spec/fixtures/test.jpg') ] }
post
end
end
Create a fixtures directory under the spec directory. I created test.jpg in a pseudo manner.
spec/models/images.rb
require "rails_helper"
RSpec.describe Image, type: :model do
let(:pic_path) { Rails.root.join('spec/fixtures/test.jpg') }
let(:pic) { Rack::Test::UploadedFile.new(pic_path) }
context "If an image is specified" do
it "Image is uploaded" do
image = create(:image)
expect(image).to be_valid
end
it "image(Multiple)But uploaded" do
image = create(:image, images: [pic, pic])
binding.pry
expect(image).to be_valid
end
end
context "If no image is specified" do
it "Get an error" do
image = build(:image, images: nil)
expect(image).to be_invalid
expect(image.errors.messages[:images]).to eq ["can't be blank"]
end
end
end
Recommended Posts