From 31d2a1198a1af88c4e6348ee89ca1e4fd8b307e8 Mon Sep 17 00:00:00 2001 From: Jennifer Wills Date: Mon, 1 Jun 2020 20:35:15 -0400 Subject: [PATCH] changed to postgres db --- Gemfile | 8 +++++--- Gemfile.lock | 11 ++++++++--- app/controllers/images_controller.rb | 24 +++++++++++++++--------- app/models/image.rb | 2 ++ app/views/images/upload.html.erb | 9 ++++++++- config/database.yml | 12 +++++++----- config/routes.rb | 2 +- db/schema.rb | 7 +++++-- 8 files changed, 51 insertions(+), 24 deletions(-) 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 @@

Upload an image

<%= form_with(url: images_create_path, method: "post", :html => {:class => "form-horizontal center"}) do |form| %> + <% if @image.errors.any? %> + + <% end %> +
<%= form.label(:files, "Choose Files:") %> - <%= form.file_field :files, multiple: true, :required => true%> + <%= form.file_field :files, multiple: true, :required => true %> + <%# , :accept => "application/jpeg, application/png, application/"%>
<%= form.label(:title, "Image Title:") %> diff --git a/config/database.yml b/config/database.yml index cc6b091..e4023ee 100644 --- a/config/database.yml +++ b/config/database.yml @@ -5,21 +5,23 @@ # gem 'sqlite3' # default: &default - adapter: sqlite3 - pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 4 } %> + adapter: postgresql + pool: 5 timeout: 5000 + username: jennifer + password: 1234 development: <<: *default - database: db/development.sqlite3 + database: development # Warning: The database defined as "test" will be erased and # re-generated from your development database when you run "rake". # Do not set this db to the same as development or production. test: <<: *default - database: db/test.sqlite3 + database: testing production: <<: *default - database: db/production.sqlite3 + database: production diff --git a/config/routes.rb b/config/routes.rb index bfdb203..511fee1 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,6 +1,6 @@ Rails.application.routes.draw do - get 'site/upload' + # get 'site/upload' get 'images/upload' post 'images/create' delete 'images/destroy_all' diff --git a/db/schema.rb b/db/schema.rb index 363634f..7b3a606 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -12,11 +12,14 @@ ActiveRecord::Schema.define(version: 2020_05_30_224924) do + # These are extensions that must be enabled in order to support this database + enable_extension "plpgsql" + create_table "active_storage_attachments", force: :cascade do |t| t.string "name", null: false t.string "record_type", null: false - t.integer "record_id", null: false - t.integer "blob_id", null: false + t.bigint "record_id", null: false + t.bigint "blob_id", null: false t.datetime "created_at", null: false t.index ["blob_id"], name: "index_active_storage_attachments_on_blob_id" t.index ["record_type", "record_id", "name", "blob_id"], name: "index_active_storage_attachments_uniqueness", unique: true