(Rails3.2.13)
paperclip 試してみる
ImageMagick をインストールしておく
sudo port install ImageMagick
下準備
$ rails new paperclip_app -T --skip-bundle $ cd paperclip_app
Gemfile
gem "paperclip"
$ bundle install $ rails g scaffold User name
db/migrate/20130430160550_create_users.rb
create_table :users do |t| t.string :name t.attachment :avatar
$ rake db:migrate
app/models/user.rb
画像を正方形にしてみる("300x300>" -> "300x300#")
class User < ActiveRecord::Base attr_accessible :name, :avatar has_attached_file :avatar, :styles => { :medium => "300x300#", :thumb => "100x100#" }, :default_url => "/images/:style/missing.png" end
app/views/users/_form.html.erb
<div class="field"> <%= f.label :name %><br /> <%= f.text_field :name %> </div> <div> <%= f.label :avatar %><br /> <%= f.file_field :avatar %> </div>
app/views/users/show.html.erb
<%= image_tag @user.avatar.url %> <%= image_tag @user.avatar.url(:medium) %> <%= image_tag @user.avatar.url(:thumb) %>