Skip to content

Commit

Permalink
changed to postgres db
Browse files Browse the repository at this point in the history
  • Loading branch information
jenniferwills committed Jun 2, 2020
1 parent 5707c6a commit 31d2a11
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 24 deletions.
8 changes: 5 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
11 changes: 8 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
24 changes: 15 additions & 9 deletions app/controllers/images_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ def index
def show
end

def upload
@image = Image.new

end

# GET /images/1/edit
def edit
Expand All @@ -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
Expand Down Expand Up @@ -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 }
Expand Down
2 changes: 2 additions & 0 deletions app/models/image.rb
Original file line number Diff line number Diff line change
@@ -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
9 changes: 8 additions & 1 deletion app/views/images/upload.html.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
<h1>Upload an image</h1>

<%= form_with(url: images_create_path, method: "post", :html => {:class => "form-horizontal center"}) do |form| %>
<% if @image.errors.any? %>
<div class="alert alert-danger" role="alert">
<%= @image.errors.full_messages.first %>
</div>
<% end %>

<div class="form-group">
<%= 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/"%>
</div>
<div class="form-group">
<%= form.label(:title, "Image Title:") %>
Expand Down
12 changes: 7 additions & 5 deletions config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
7 changes: 5 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 31d2a11

Please sign in to comment.