I'm developing a small Ruby on Rails application for a small shop which sells used cars. The site contains a list with all avalaible cars including an image of the car. There are at maximum 20 cars in the shop.
I read that it is not the best practice to save images directly in the database and it's better to store the files with a cloud storage service like Amazon Web Services, but I don't think that a service like this is necessary for a small website with a maximum of 20 images at a time.
My idea is to save the images in /app/assets/images
. Employees will upload information and images of the cars through a form. Is it possible to upload and save images in the folder /app/assets/images
through a form in Rails?
As @Sergio already mentioned about assets folder and AWS S3 service.
AWS is good, But limited only for 1 year free tier and upto 5GB.
Alternate solution for cloud image storage is Cloudinary. It's lifetime free <10GB, bandwidth included, Link: How to use in Rails samples
Also Cloudinary uses AWS(S3) services, behind the scene!
#Gemfile
gem 'cloudinary'
gem 'carrierwave'
# config/initializers/cloudinary.rb, if not exist create the file
# make sure to fill api keys!!!
Cloudinary.config do |config|
config.cloud_name = ....
config.api_key = ....
config.api_secret = ....
config.cdn_subdomain = false
end
# model/image.rb in my case.
class Image < ActiveRecord::Base
mount_uploader :image, ImageUploader
end
# app/uploaders/image_uploader.rb
class ImageUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
include CarrierWave::ImageOptimizer
include Cloudinary::CarrierWave
process :convert => 'png'
# process :tags => ['team_registration']
# process :resize_to_fill => [300, 300, :north]
def store_dir
# "images/#{model.class.to_s.underscore}/#{model.team_name}"
"images/car_images/"
end
def public_id
"yr_domain_name_here/car_images/#{model.team_name}"
end
def cache_dir
"/tmp/cache/#{model.class.to_s.underscore}"
end
def filename
"#{model.team_name}_#{model.game.title}" + ".png" if original_filename.present?
end
def content_type_whitelist
/image\//
end
def extension_white_list
%w(jpg jpeg gif png)
end
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With