Skip to content

Commit

Permalink
Add Paperclip Gem
Browse files Browse the repository at this point in the history
  • Loading branch information
Neil Gehani committed Sep 19, 2013
1 parent 89454ce commit 4ad366b
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 10 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ doc/
.project
.DS_Store
.idea

# Ignore Paperclip uploaded images
/public/system
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ gem 'rails', '4.0.0'
gem 'jquery-rails'
gem 'devise'
gem 'simple_form', '~> 3.0.0.rc'
gem "paperclip", "~> 3.0"

group :production do
gem 'pg'
Expand Down
10 changes: 10 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ GEM
bootstrap-sass (2.3.2.2)
sass (~> 3.2)
builder (3.1.4)
climate_control (0.0.3)
activesupport (>= 3.0)
cocaine (0.5.1)
climate_control (>= 0.0.3, < 1.0)
coffee-rails (4.0.0)
coffee-script (>= 2.2.0)
railties (>= 4.0.0.beta, < 5.0)
Expand Down Expand Up @@ -61,6 +65,11 @@ GEM
minitest (4.7.5)
multi_json (1.8.0)
orm_adapter (0.4.0)
paperclip (3.5.1)
activemodel (>= 3.0.0)
activesupport (>= 3.0.0)
cocaine (~> 0.5.0)
mime-types
pg (0.17.0)
polyglot (0.3.3)
protected_attributes (1.0.3)
Expand Down Expand Up @@ -135,6 +144,7 @@ DEPENDENCIES
devise
jbuilder (= 1.0.2)
jquery-rails
paperclip (~> 3.0)
pg
protected_attributes
rails (= 4.0.0)
Expand Down
10 changes: 7 additions & 3 deletions app/models/pin.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
class Pin < ActiveRecord::Base
attr_accessible :description
attr_accessible :description, :image
has_attached_file :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 => "300x300>" }

validates :user_id, presence: true
end
3 changes: 3 additions & 0 deletions app/views/pins/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +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" } %>

<div class="form-actions">
Expand Down
13 changes: 7 additions & 6 deletions app/views/pins/_pin.html.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@

<tr>
<td><%= pin.description %></td>
<td><%= link_to 'Show', pin %></td>
<% if current_user == pin.user %>
<td><%= link_to 'Edit', edit_pin_path(pin) %></td>
<td><%= link_to 'Destroy', pin, method: :delete, data: { confirm: 'Are you sure?' } %></td>
<% end %>
<td><%= image_tag pin.image(:medium) %></td>
<td><%= pin.description %></td>
<td><%= link_to 'Show', pin %></td>
<% if current_user == pin.user %>
<td><%= link_to 'Edit', edit_pin_path(pin) %></td>
<td><%= link_to 'Destroy', pin, method: :delete, data: { confirm: 'Are you sure?' } %></td>
<% end %>
</tr>
1 change: 1 addition & 0 deletions app/views/pins/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<table class="table table-striped">
<thead>
<tr>
<th>Image</th>
<th>Description</th>
<th></th>
<th></th>
Expand Down
1 change: 1 addition & 0 deletions app/views/pins/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<div class="row">
<div class="span6 offset3">
<div class="well">
<%= image_tag @pin.image %>
<p>
<%= @pin.description %>
</p>
Expand Down
11 changes: 11 additions & 0 deletions db/migrate/20130919053303_add_attachment_image_to_pins.rb
Original file line number Diff line number Diff line change
@@ -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
6 changes: 5 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20130919041835) do
ActiveRecord::Schema.define(version: 20130919053303) do

create_table "pins", force: true do |t|
t.string "description"
t.datetime "created_at"
t.datetime "updated_at"
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"
Expand Down

0 comments on commit 4ad366b

Please sign in to comment.