Skip to content
Browse files

Subir ficheros con Paperclip

  • Loading branch information...
1 parent f8ab757 commit 70608e5996b04f77d5a3794b4d9e40deef9da2dc @jquemada jquemada committed Apr 11, 2012
View
4 .gitignore
@@ -16,3 +16,7 @@
# ignore al .DS_store files of MAC OSX
.DS_Store
+
+# ignore paperclip installation files
+public/system
+.project
View
1 Gemfile
@@ -23,6 +23,7 @@ end
gem 'jquery-rails'
gem 'cleditor_rails'
+gem 'paperclip'
# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'
View
7 Gemfile.lock
@@ -32,6 +32,7 @@ GEM
bcrypt-ruby (3.0.1)
builder (3.0.0)
cleditor_rails (0.0.3)
+ cocaine (0.2.1)
coffee-rails (3.2.2)
coffee-script (>= 2.2.0)
railties (~> 3.2.0)
@@ -61,6 +62,11 @@ GEM
mime-types (1.17.2)
multi_json (1.1.0)
orm_adapter (0.0.7)
+ paperclip (2.4.5)
+ activerecord (>= 2.3.0)
+ activesupport (>= 2.3.2)
+ cocaine (>= 0.0.2)
+ mime-types
polyglot (0.3.3)
rack (1.4.1)
rack-cache (1.2)
@@ -117,6 +123,7 @@ DEPENDENCIES
coffee-rails (~> 3.2.1)
devise
jquery-rails
+ paperclip
rails (= 3.2.2)
sass-rails (~> 3.2.3)
sqlite3
View
5 app/models/site.rb
@@ -2,12 +2,13 @@ class Site < ActiveRecord::Base
belongs_to :type
belongs_to :user
has_many :visits
- has_many :trips, :through => :visits
+ has_many :trips, :through => :visits
+ has_attached_file :image
# Debe estar protegido para evitar accesos indeseados
attr_protected :user_id
# Se añaden estas definiciones
- validates :name, :type_id, :image_url, :presence => true # campo obligatorio
+ validates :name, :type_id, :presence => true # campo obligatorio
end
View
4 app/views/sites/_form.html.erb
@@ -24,8 +24,8 @@
<%= f.collection_select(:type_id, Type.find(:all, :order => :name), :id, :name) %>
</div>
<div class="field">
- <%= f.label :image_url %><br />
- <%= f.text_field :image_url %>
+ <%= f.label :image %><br />
+ <%= f.file_field :image %>
</div>
<div class="actions">
<%= f.submit %>
View
2 app/views/sites/index.html.erb
@@ -6,7 +6,7 @@
<tr class="<%= cycle('list_line_odd', 'list_line_even') %>">
<td>
- <%= link_to image_tag(site.image_url, :class => 'list_image'), site %>
+ <%= link_to image_tag(site.image.url, :class => 'list_image'), site %>
</td>
<td class="list_description">
View
2 app/views/sites/show.html.erb
@@ -2,7 +2,7 @@
<h1><%= @site.type.name if @site.type %></h1>
- <%= image_tag(@site.image_url, :class => 'site_image') %>
+ <%= image_tag(@site.image.url, :class => 'site_image') %>
<h3><%= @site.name %></h3>
View
2 app/views/trips/_trip.html.erb
@@ -3,7 +3,7 @@
<% trip.visits.order(:hour).each do |visit| %>
<tr class="<%= cycle('list_line_odd', 'list_line_even') %>">
<td>
- <%= link_to image_tag(visit.site.image_url, :class => 'list_image'), visit.site %>
+ <%= link_to image_tag(visit.site.image.url, :class => 'list_image'), visit.site %>
</td>
<td class="list_description">
View
19 db/migrate/20120411095828_add_paperclip_to_sites.rb
@@ -0,0 +1,19 @@
+class AddPaperclipToSites < ActiveRecord::Migration
+ def up
+ change_table :sites do |t|
+ t.column :image_file_name, :string
+ t.column :image_content_type, :string
+ t.column :image_file_size, :string
+ t.column :image_updated_at, :datetime
+ end
+ end
+
+ def down
+ change_table :sites do |t|
+ t.remove :image_file_name, :string
+ t.remove :image_content_type, :string
+ t.remove :image_file_size, :string
+ t.remove :image_updated_at, :datetime
+ end
+ end
+end
View
18 db/schema.rb
@@ -11,16 +11,20 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120409085740) do
+ActiveRecord::Schema.define(:version => 20120411095828) do
create_table "sites", :force => true do |t|
t.string "name"
t.text "description"
t.integer "type_id"
t.string "image_url"
- 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.string "image_file_size"
+ t.datetime "image_updated_at"
end
create_table "trips", :force => true do |t|
@@ -58,4 +62,12 @@
add_index "users", ["email"], :name => "index_users_on_email", :unique => true
add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true
+ create_table "visits", :force => true do |t|
+ t.integer "trip_id"
+ t.integer "site_id"
+ t.integer "hour"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
+
end

0 comments on commit 70608e5

Please sign in to comment.
Something went wrong with that request. Please try again.