Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails - Is saving images through form in /app/assets/images possible?

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?

like image 705
Vega180 Avatar asked Mar 06 '23 16:03

Vega180


1 Answers

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
like image 98
7urkm3n Avatar answered Apr 28 '23 17:04

7urkm3n