diff --git a/Gemfile b/Gemfile index 7f0eec6..bb85934 100644 --- a/Gemfile +++ b/Gemfile @@ -7,6 +7,7 @@ ruby '2.7.0' gem 'rails', '~> 6.0.3', '>= 6.0.3.1' # Use sqlite3 as the database for Active Record gem 'sqlite3', '~> 1.4' +gem 'pg' # Use Puma as the app server gem 'puma', '~> 4.1' # Use SCSS for stylesheets @@ -26,9 +27,10 @@ gem 'jbuilder', '~> 2.7' # gem 'image_processing', '~> 1.2' gem 'bootstrap', '~> 4.3.1' -gem 'jquery-rails' -gem 'image_processing' -gem 'active_storage-send_zip' +gem 'jquery-rails', '~> 4.4.0' +gem 'image_processing', '~> 1.11.0' +gem 'active_storage-send_zip', '~> 0.3.3' +gem 'active_storage_validations', '~> 0.8.9' # Reduces boot times through caching; required in config/boot.rb gem 'bootsnap', '>= 1.4.2', require: false diff --git a/Gemfile.lock b/Gemfile.lock index 266e132..6018a2b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -40,6 +40,8 @@ GEM active_storage-send_zip (0.3.3) rails (> 5.2) rubyzip (< 3.0) + active_storage_validations (0.8.9) + rails (>= 5.2.0) activejob (6.0.3.1) activesupport (= 6.0.3.1) globalid (>= 0.3.6) @@ -119,6 +121,7 @@ GEM nio4r (2.5.2) nokogiri (1.10.9) mini_portile2 (~> 2.4.0) + pg (1.2.3) popper_js (1.16.0) public_suffix (4.0.5) puma (4.3.5) @@ -213,15 +216,17 @@ PLATFORMS ruby DEPENDENCIES - active_storage-send_zip + active_storage-send_zip (~> 0.3.3) + active_storage_validations (~> 0.8.9) bootsnap (>= 1.4.2) bootstrap (~> 4.3.1) byebug capybara (>= 2.15) - image_processing + image_processing (~> 1.11.0) jbuilder (~> 2.7) - jquery-rails + jquery-rails (~> 4.4.0) listen (~> 3.2) + pg puma (~> 4.1) rails (~> 6.0.3, >= 6.0.3.1) sass-rails (>= 6) diff --git a/app/controllers/images_controller.rb b/app/controllers/images_controller.rb index 10ab76b..4a61389 100644 --- a/app/controllers/images_controller.rb +++ b/app/controllers/images_controller.rb @@ -14,6 +14,10 @@ def index def show end + def upload + @image = Image.new + + end # GET /images/1/edit def edit @@ -23,18 +27,24 @@ def edit # POST /images.json def create parameters =create_params + errors = [] parameters[:files].each do |f| tmp = {:title => parameters[:title], :private => parameters[:private], :tags => parameters[:tags], :file => f} + # byebug + + @image = Image.create(tmp) + # byebug - @image = Image.create!(tmp) if !(@image.save) - format.html { render :new } - format.json { render json: @image.errors, status: :unprocessable_entity } + # byebug + render action: :upload end end - redirect_to root_path, notice: 'Image was successfully created.' + if errors.empty? + redirect_to root_path, notice: 'Image was successfully created.' + end end # PATCH/PUT /images/1 @@ -63,11 +73,7 @@ def destroy end def destroy_all - imgs = Image.all - imgs.each do |img| - img.file.purge - img.destroy - end + Image.destroy_all respond_to do |format| format.html { redirect_to root_path, notice: 'Image was successfully destroyed.' } format.json { head :no_content } diff --git a/app/models/image.rb b/app/models/image.rb index 8790170..31e9eaa 100644 --- a/app/models/image.rb +++ b/app/models/image.rb @@ -1,4 +1,6 @@ class Image < ApplicationRecord has_one_attached :file + validates :file, attached: true, content_type: ['image/png', 'image/jpg', 'image/jpeg'], size: { less_than: 10.megabytes , message: 'is too big' } + end diff --git a/app/views/images/upload.html.erb b/app/views/images/upload.html.erb index 23caae5..248e178 100644 --- a/app/views/images/upload.html.erb +++ b/app/views/images/upload.html.erb @@ -1,9 +1,16 @@