Permalink
Browse files

[#6] Add avatars to friends

  • Loading branch information...
1 parent 2ef79cc commit 8773776d50cc23b2e1a22a67f105039bdd7484cd @harlow harlow committed Aug 2, 2012
View
@@ -7,9 +7,13 @@
# Ignore bundler config
/.bundle
+# Ignore images uploaded in Test and Development environments
+/public/system
+
# Ignore the default SQLite database.
/db/*.sqlite3
# Ignore all logfiles and tempfiles.
/log/*.log
/tmp
+*.swp
@@ -1,3 +1,9 @@
class Friend < ActiveRecord::Base
- attr_accessible :name
+ attr_accessible :name, :avatar
+ validates_presence_of :name
+
+ has_attached_file :avatar, styles: {
+ thumb: '100x100>',
+ medium: '300x300>'
+ }
end
@@ -15,6 +15,10 @@
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
+ <div class="field">
+ <%= f.label :avatar %><br />
+ <%= f.file_field :avatar %>
+ </div>
<div class="actions">
<%= f.submit %>
</div>
@@ -2,6 +2,7 @@
<table>
<tr>
+ <th>Avatar</th>
<th>Name</th>
<th></th>
<th></th>
@@ -10,6 +11,7 @@
<% @friends.each do |friend| %>
<tr>
+ <td><%= image_tag friend.avatar.url(:thumb) %>
<td><%= friend.name %></td>
<td><%= link_to 'Show', friend %></td>
<td><%= link_to 'Edit', edit_friend_path(friend) %></td>
@@ -1,6 +1,7 @@
<p id="notice"><%= notice %></p>
<p>
+ <%= image_tag @friend.avatar.url(:medium) %><br />
<b>Name:</b>
<%= @friend.name %>
</p>
@@ -0,0 +1,9 @@
+class AddAvatarToFriends < ActiveRecord::Migration
+ def self.up
+ add_attachment :friends, :avatar
+ end
+
+ def self.down
+ remove_attachment :friends, :avatar
+ end
+end
View
@@ -11,12 +11,16 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120802190047) do
+ActiveRecord::Schema.define(:version => 20120802191046) do
create_table "friends", :force => true do |t|
t.string "name"
- 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.string "avatar_file_name"
+ t.string "avatar_content_type"
+ t.integer "avatar_file_size"
+ t.datetime "avatar_updated_at"
end
end
@@ -0,0 +1,14 @@
+require 'spec_helper'
+
+feature 'visitor adds a new friend' do
+ scenario 'add friend with image' do
+ visit root_path
+ click_link 'New Friend'
+ fill_in 'Name', with: 'Mona Lisa'
+ attach_file 'Avatar', Rails.root.join('spec', 'support', 'mona_lisa.jpg')
+ click_button 'Create Friend'
+
+ page.should have_css('p', text: 'Mona Lisa')
+ page.should have_xpath("//img[contains(@src, 'mona_lisa.jpg')]")
+ end
+end
@@ -0,0 +1,5 @@
+require 'spec_helper'
+
+describe Friend do
+ it { should validate_presence_of :name }
+end
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 8773776

Please sign in to comment.