目次
環境
インストール
実装
テスト
参考
Ruby: 2.0.0-p0
Rails: 3.2.13
paperclip: 3.4.1
ImageMagickが必要なので, 確認して無かったらインストールしておく.
1
2
which convert # 無かったら次でインストール
brew install imagemagick
後はいつも通り, Gemfileにgem 'paperclip'
を追加してbundle install
すればよい.
既存のUserモデルにアイコンとしてavatarを追加し, 登録, 編集, 表示が出来るようにする.
まずは, rails g paperclip user avatar
してrake db:migrate
しておく.
生成されたマイグレーションファイルの中身はこんな感じ.
1
2
3
4
5
6
7
8
9
10
11
class AddAttachmentAvatarToUsers < ActiveRecord : :Migration
def self . up
change_table :users do | t |
t . attachment :avatar
end
end
def self . down
drop_attached_file :users , :avatar
end
end
後は実装していく. app/models/user.rb
に以下を追加.
1
2
3
4
5
6
7
8
class User < ActiveRecord : :Base
attr_accessible :avatar
has_attached_file :avatar , styles : { medium : "300x300>" , thumb : "100x100>" }, default_url : "/system/missing/:style/missing.jpg"
validates_attachment :avatar , presence : true ,
content_type : { content_type : [ "image/jpg" , "image/png" ] },
size : { less_than : 2 . megabytes }
end
app/views/users/_form.html.erb
では, <%= form_for(@user) do |f| %>
の中に
1
2
3
4
<div class="field">
<%= f . label :avatar %> <br />
<%= f . file_field :avatar %>
</div>
を追加. app/views/users/show.html.erb
では, 画像を表示させたい箇所に
1
2
3
<p>
<%= image_tag @user . avatar . url ( :thumb ) %>
</p>
を追加. これだけで登録, 編集, 表示が出来るようになった.
まず, spec/spec_helper.rb
に以下を追加しておく.
1
2
3
4
5
require "paperclip/matchers"
RSpec . configure do | config |
config . include Paperclip : :Shoulda :: Matchers
end
spec/models/user_spec.rb
はこんな感じ.
1
2
3
4
5
6
7
8
describe User do
it { should have_attached_file ( :avatar ) }
it { should validate_attachment_presence ( :avatar ) }
it { should validate_attachment_content_type ( :avatar ) .
allowing ( 'image/jpg' , 'image/png' ) }
it { should validate_attachment_size ( :avatar ) .
less_than ( 2 . megabytes ) }
end
spec/controllers/users_controller_spec.rb
はこんな感じになる.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
describe UsersController do
def valid_attributes
{ username : "MyString" ,
email : "MyString" ,
avatar_file_name : '私のテキスト_n' ,
avatar_content_type : 'image/png' ,
avatar_file_size : 1 ,
avatar_updated_at : '2011-07-13 14:53:38'
}
end
def valid_session
{}
end
describe "GET show" do
it "assigns the requested user as @user" do
user = User . create! valid_attributes
get :show , { :id => user . to_param }, valid_session
assigns ( :user ) . should eq ( user )
end
end
end