Skip to content

Commit

Permalink
add paperclip
Browse files Browse the repository at this point in the history
  • Loading branch information
ihower committed Oct 5, 2015
1 parent 5ec5b89 commit 7de1031
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 4 deletions.
4 changes: 3 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ source 'https://rubygems.org'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.4'

gem "paperclip"

# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# Use SCSS for stylesheets
Expand Down Expand Up @@ -44,4 +47,3 @@ group :development do
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
end

12 changes: 12 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ GEM
debug_inspector (>= 0.0.1)
builder (3.2.2)
byebug (6.0.2)
climate_control (0.0.3)
activesupport (>= 3.0)
cocaine (0.5.7)
climate_control (>= 0.0.3, < 1.0)
coffee-rails (4.1.0)
coffee-script (>= 2.2.0)
railties (>= 4.0.0, < 5.0)
Expand All @@ -67,11 +71,18 @@ GEM
mail (2.6.3)
mime-types (>= 1.16, < 3)
mime-types (2.6.2)
mimemagic (0.3.0)
mini_portile (0.6.2)
minitest (5.8.0)
multi_json (1.11.2)
nokogiri (1.6.6.2)
mini_portile (~> 0.6.0)
paperclip (4.3.0)
activemodel (>= 3.2.0)
activesupport (>= 3.2.0)
cocaine (~> 0.5.5)
mime-types
mimemagic (= 0.3.0)
rack (1.6.4)
rack-test (0.6.3)
rack (>= 1.0)
Expand Down Expand Up @@ -143,6 +154,7 @@ DEPENDENCIES
coffee-rails (~> 4.1.0)
jbuilder (~> 2.0)
jquery-rails
paperclip
rails (= 4.2.4)
sass-rails (~> 5.0)
sdoc (~> 0.4.0)
Expand Down
9 changes: 7 additions & 2 deletions app/controllers/conferences_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def index
elsif params[:sort] == "attendees_count"
@events = @events.order("attendees_count desc")
elsif params[:sort] == "last_registered_at"
@events = @events.order("last_registered_at desc")
@events = @events.order("last_registered_at desc")
end

if params[:event_id]
Expand All @@ -41,6 +41,11 @@ def create

def update
@event = Event.find(params[:id])

if params[:destroy_logo] == "1"
@event.logo = nil
end

if @event.update(event_params)
redirect_to conferences_path
else
Expand All @@ -59,7 +64,7 @@ def destroy
private

def event_params
params.require(:event).permit(:name, :description, :url, :category_id, :group_ids => [] )
params.require(:event).permit(:name, :description, :url, :category_id, :logo, :group_ids => [] )
end

end
3 changes: 3 additions & 0 deletions app/models/event.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
class Event < ActiveRecord::Base

has_attached_file :logo, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "/images/:style/missing.png"
validates_attachment_content_type :logo, content_type: /\Aimage\/.*\Z/

validates_presence_of :name

belongs_to :category
Expand Down
10 changes: 10 additions & 0 deletions app/views/conferences/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@
<%= f.label :description %>
<%= f.text_area :description %>

<div class="form-group">
<%= f.label :logo, "LOGO檔案" %>
<%= f.file_field :logo %>
<% if @event.logo.exists? %>
<%= check_box_tag "destroy_logo", "1" %> 刪除檔案
<% end %>
</div>

<%= f.submit "送出" %>
<% end %>
Expand Down
5 changes: 5 additions & 0 deletions app/views/conferences/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
<%= @event.name %>
<% if @event.logo.exists? %>
<%= link_to image_tag(@event.logo.url(:thumb)),
@event.logo.url %>
<% end %>
<%= simple_format( @event.description ) %>
<%= link_to "Back to index", conferences_path %>
7 changes: 7 additions & 0 deletions db/migrate/20151005021311_add_paperclip_to_event.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class AddPaperclipToEvent < ActiveRecord::Migration

def change
add_attachment :events, :logo
end

end
6 changes: 5 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20150930041524) do
ActiveRecord::Schema.define(version: 20151005021311) do

create_table "attendees", force: :cascade do |t|
t.integer "event_id"
Expand All @@ -37,6 +37,10 @@
t.integer "category_id"
t.integer "attendees_count", default: 0
t.datetime "last_registered_at"
t.string "logo_file_name"
t.string "logo_content_type"
t.integer "logo_file_size"
t.datetime "logo_updated_at"
end

add_index "events", ["category_id"], name: "index_events_on_category_id"
Expand Down

0 comments on commit 7de1031

Please sign in to comment.