2007-11-10 [Ruby on Rails]ファイルアップロードプラグイン attachment_fu
■[Ruby on Rails]
ファイルをUPLOADする際に利用できるプラグインの覚書。
http://wiki.rubyonrails.org/rails/pages/HowtoUploadFiles
を見ると基本的なuploadを行う際の手順が記載されている。
その中でページ下段「Other approaches」を見ると、いくつかplugin形式のものも含まれている。
■file_column
■Acts as Attachment
■attachment_fu
その中でfile_columnは長い間更新されていないのでパス。今回はActs as Attachmentの後継?のようなattachment_fuを試してみる。
親切なTutorialがhttp://clarkware.com/cgi/blosxom/2007/02/24に用意されている。*1
attachment_fuプラグインの導入
前提としてRMagickをインストールしておいてください。*2
$ script/plugin install http://svn.techno-weenie.net/projects/plugins/attachment_fu/
■model・migrationの作成
アップロードデータを扱うモデルを作成
$ script/generate model mugshot
続いてmigration。
このあたりはActs as Attachmentと同じです。*3
db/migrate/002_create_mugshots.rb
class CreateMugshots < ActiveRecord::Migration def self.up create_table :mugshots do |t| t.column :parent_id, :integer t.column :content_type, :string t.column :filename, :string t.column :thumbnail, :string t.column :size, :integer t.column :width, :integer t.column :height, :integer end end def self.down drop_table :mugshots end end
$ rake db:migrate --trace (in /cygdrive/c/workspace/sample_attachment_fo) ** Invoke db:migrate (first_time) ** Invoke environment (first_time) ** Execute environment ** Execute db:migrate == CreateMugshots: migrating ========================================== -- create_table(:mugshots) -> 0.2670s == CreateMugshots: migrated (0.2670s) ================================= ** Invoke db:schema:dump (first_time) ** Invoke environment ** Execute db:schema:dump
その後modelの編集を行います。
app/models/mushots.rb
class Mugshot < ActiveRecord::Base has_attachment :content_type => :image, :storage => :file_system, :max_size => 3000.kilobytes, # :resize_to => '320x200>', :thumbnails => { :thumb => '100x100>', :small => '50x50>' } validates_as_attachment end
ここではいくつかオプションで指定できます。
:content_type ファイルタイプ指定。イメージファイルだったり、後はワードだけ等々ある程度指定できる
:storage データを保存する方法を指定できます。デフォルトではDBにバイナリ保存するみたい(試してない)。後は今回利用しているfile_system。特徴的なのがAWSを簡単に利用できるみたい。(だけどこれも試してない)
:max_size 名前のとおり。ファイルの最大容量指定
:min_size 名前のとおり。ファイルの最小容量指定
:resize_to これを指定しておくとデフォルトでリサイズされる
:thumbnails これを指定しておくといくつかサムネイル用にリサイズしたデータを作成してくれる。
まだまだ他にもオプションはあるけど試してないものはパス。
controllerの生成
$ script/generate controller main new create index
class MainController < ApplicationController def new @mugshot = Mugshot.new end def create @mugshot = Mugshot.new(params[:mugshot]) if @mugshot.save flash[:notice] = 'Mugshot was successfully created.' redirect_to '/main/index' else render :action => :new end end def index @mugshots = Mugshot.find(:all, :conditions => ["parent_id is null"] ) end end
viewの作成
app/views/main/index.rhtml
<h1>Sample Index</h1> <% for mugshot in @mugshots -%> <%= link_to image_tag(mugshot.public_filename(:small)), mugshot.public_filename %> <% end -%> <%= link_to 'new', {:action => :new} %>
mugshot.public_filename(:small)とあるところで、実際のアップロードデータにアクセスできるようになっている。
app/views/main/new.rhtml
<%= error_messages_for :mugshot %> <% form_for(:mugshot, :url => 'create', :html => { :multipart => true }) do |f| -%> <p> <label for="mugshot">Upload A Mugshot:</label> <%= f.file_field :uploaded_data %> </p> <p> <%= submit_tag 'Create' %> </p> <% end -%> <%= link_to 'index', {:action => :index} %>
以上。とりあえずこれを少し修正したもの(見た目+ユーザ認証くらいはいるかな・・)をせっかく借りたのでRailsPlayGroudにあげてみようか検討中。
*1:なので本当に覚書
*2:ImageScience、minimagickでもいいみたいです。RMagickのインストール方法についてはWEB上にたくさんの情報があるので省略
*3:ちょっとしっかり確認してませんが前見たときこんな感じだったと思います。
- 2 http://d.hatena.ne.jp/keyworddiary/Ruby
- 2 http://www.google.co.jp/search?q=ubuntu+ruby+on+rails&sourceid=navclient-ff&ie=UTF-8&rlz=1B2DVFC_jaJP226JP226
- 1 http://72.14.235.104/search?q=cache:bQGJ5nWb_14J:d.hatena.ne.jp/cuspos/20070515+no-ip.conf+ubuntu&hl=en&ct=clnk&cd=1&gl=jp&lr=lang_ja&client=firefox
- 1 http://a.hatena.ne.jp/ath/
- 1 http://d.hatena.ne.jp/cuspos
- 1 http://d.hatena.ne.jp/diarylist?of=0&mode=rss&type=public
- 1 http://d.hatena.ne.jp/keyword/検討中
- 1 http://d.hatena.ne.jp/keyword/ActiveRecord
- 1 http://d.hatena.ne.jp/keyword/Ruby
- 1 http://d.hatena.ne.jp/masataka_k/20071110/1194701530