ぽんこつメモ

https://github.com/kanorimon

railsアプリ開発(paperclip)

imagemagickインストール

# yum install ImageMagick ImageMagick-devel

CentOS6系なのでyum、最新を使用する場合はソースからコンパイル

paperclipインストール

# vi Gemfile

gem "paperclip", "~> 3.0"

# vi config/enviroments/development.rb

Paperclip.options[:command_path] = "/usr/bin/"

model更新

attr_accessible :avatar

has_attached_file :avatar, :styles => { :thumb => "50x50>" }

migration追加

class AddAvatarToUsers < ActiveRecord::Migration

  def self.up

    add_attachment :users, :avatar

  end

  def self.down

    remove_attachment :users, :avatar

  end

end

URLからアップロード

require 'open-uri'

user.avatar = open(auth["info"]["image"])

アップロード時のファイル名指定

before_post_process :transliterate_file_name

def transliterate_file_name

  extension = 'jpg'

  filename = self.nickname

  self.avatar.instance_write(:file_name, "#{filename}.#{extension}")

end

メモ

has_attached_file :avatar,

  :path => ":rails_root/public/system/:attachment/:id/:style/:filename",

  :url => "/system/:attachment/:id/:style/:filename"