From acd606ead51e04dcba0f149450059e9328c1c0ca Mon Sep 17 00:00:00 2001 From: Andrew Cockerham Date: Tue, 4 Jun 2013 06:21:06 -0500 Subject: [PATCH] added Paperclip Gem --- .gitignore | 5 ++++- Gemfile | 2 +- Gemfile.lock | 11 +++++++++++ app/controllers/pages_controller.rb | 2 +- app/models/pin.rb | 8 ++++++-- app/views/pins/_form.html.erb | 4 +++- app/views/pins/_pin.html.erb | 1 + app/views/pins/index.html.erb | 1 + app/views/pins/show.html.erb | 1 + .../20130604110107_add_attachment_image_to_pins.rb | 11 +++++++++++ db/schema.rb | 10 +++++++--- 11 files changed, 47 insertions(+), 9 deletions(-) create mode 100644 db/migrate/20130604110107_add_attachment_image_to_pins.rb diff --git a/.gitignore b/.gitignore index 6501215..e54dde9 100644 --- a/.gitignore +++ b/.gitignore @@ -18,4 +18,7 @@ doc/ *~ .project .DS_Store -.idea \ No newline at end of file +.idea + +# Ignore Paperclip uploaded files +/public/system \ No newline at end of file diff --git a/Gemfile b/Gemfile index 57f5932..221e070 100644 --- a/Gemfile +++ b/Gemfile @@ -4,6 +4,7 @@ gem 'rails', '3.2.12' gem 'jquery-rails' gem 'devise' gem 'simple_form' +gem "paperclip", "~> 3.0" # Bundle edge Rails instead: # gem 'rails', :git => 'git://github.com/rails/rails.git' @@ -14,7 +15,6 @@ end group :development, :test do gem 'sqlite3' - gem 'webrick', '~> 1.3.1' end # Gems used only for assets and not required diff --git a/Gemfile.lock b/Gemfile.lock index a1cf7fc..6aaf074 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -33,6 +33,10 @@ GEM bootstrap-sass (2.3.1.0) sass (~> 3.2) builder (3.0.4) + climate_control (0.0.3) + activesupport (>= 3.0) + cocaine (0.5.1) + climate_control (>= 0.0.3, < 1.0) coffee-rails (3.2.2) coffee-script (>= 2.2.0) railties (~> 3.2.0) @@ -62,6 +66,12 @@ GEM mime-types (1.23) multi_json (1.7.2) orm_adapter (0.4.0) + paperclip (3.4.2) + activemodel (>= 3.0.0) + activerecord (>= 3.0.0) + activesupport (>= 3.0.0) + cocaine (~> 0.5.0) + mime-types pg (0.15.1) polyglot (0.3.3) rack (1.4.5) @@ -123,6 +133,7 @@ DEPENDENCIES coffee-rails (~> 3.2.1) devise jquery-rails + paperclip (~> 3.0) pg rails (= 3.2.12) sass-rails (~> 3.2.3) diff --git a/app/controllers/pages_controller.rb b/app/controllers/pages_controller.rb index 1f5c01c..b20bb4d 100644 --- a/app/controllers/pages_controller.rb +++ b/app/controllers/pages_controller.rb @@ -21,6 +21,6 @@ def answer def vip end - def new_job_post + def job_post end end diff --git a/app/models/pin.rb b/app/models/pin.rb index b054a14..32644cd 100644 --- a/app/models/pin.rb +++ b/app/models/pin.rb @@ -1,9 +1,13 @@ class Pin < ActiveRecord::Base - attr_accessible :description + attr_accessible :description, :image validates :description, presence: true + validates :user_id, presence: true + validates_attachment :image, presence: true, + content_type: { content_type: ['image/jpeg', 'image/jpg', 'image/png', 'image/gif'] }, + size: { less_than: 5.megabytes } belongs_to :user + has_attached_file :image, styles: { medium: "320x240>" } - validates :user_id, presence: true end diff --git a/app/views/pins/_form.html.erb b/app/views/pins/_form.html.erb index 95886fd..10882f0 100644 --- a/app/views/pins/_form.html.erb +++ b/app/views/pins/_form.html.erb @@ -1,7 +1,9 @@ <%= simple_form_for(@pin, html: { class: "form-horizontal"}) do |f| %> <%= f.error_notification %> + <%= f.full_error :image_file_size, class: "alert alert-error" %> +<%= f.full_error :image_content_type, class: "alert alert-error" %> - + <%= f.input :image, label: "Upload an image" %> <%= f.input :description, as: :text, input_html: { rows: "3" } %> diff --git a/app/views/pins/_pin.html.erb b/app/views/pins/_pin.html.erb index 1cc1094..45eefe1 100644 --- a/app/views/pins/_pin.html.erb +++ b/app/views/pins/_pin.html.erb @@ -1,4 +1,5 @@ + <%= image_tag pin.image(:medium) %> <%= pin.description %> <%= link_to 'Show', pin %> <% if current_user == pin.user %> diff --git a/app/views/pins/index.html.erb b/app/views/pins/index.html.erb index f7e80d9..f64ffa7 100644 --- a/app/views/pins/index.html.erb +++ b/app/views/pins/index.html.erb @@ -3,6 +3,7 @@ + diff --git a/app/views/pins/show.html.erb b/app/views/pins/show.html.erb index c584ed8..8c61ae5 100644 --- a/app/views/pins/show.html.erb +++ b/app/views/pins/show.html.erb @@ -1,6 +1,7 @@
+ <%= image_tag @pin.image %>

<%= @pin.description %>

diff --git a/db/migrate/20130604110107_add_attachment_image_to_pins.rb b/db/migrate/20130604110107_add_attachment_image_to_pins.rb new file mode 100644 index 0000000..1f192cb --- /dev/null +++ b/db/migrate/20130604110107_add_attachment_image_to_pins.rb @@ -0,0 +1,11 @@ +class AddAttachmentImageToPins < ActiveRecord::Migration + def self.up + change_table :pins do |t| + t.attachment :image + end + end + + def self.down + drop_attached_file :pins, :image + end +end diff --git a/db/schema.rb b/db/schema.rb index cdd7ec4..e689987 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,13 +11,17 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20130602172033) do +ActiveRecord::Schema.define(:version => 20130604110107) do create_table "pins", :force => true do |t| t.string "description" - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false t.integer "user_id" + t.string "image_file_name" + t.string "image_content_type" + t.integer "image_file_size" + t.datetime "image_updated_at" end add_index "pins", ["user_id"], :name => "index_pins_on_user_id"
Image Description