2010-10-09

file_columnでファイルのアップデート

[目的]画像のアップデート
[環境]Rails3.0, ruby1.8.7

(1)ImageMagickのインストール
 
sudo port install ImageMagick

--あとは、関連エントリのそのまま実行

(2)プロジェクト作成

rails new file_column_test

(3)file_columnプラグインインストール


cd file_column_test
rails plugin install git://github.com/tekin/file_column.git


(4)パッチ適用

vendor/plugins/file_column/lib/file_column.rb
Rails.rootを変更。


--- file_column.rb.orig 2010-09-28 00:54:44.000000000 +0900
+++ file_column.rb 2010-09-28 00:55:12.000000000 +0900
@@ -594,7 +594,7 @@

# default options. You can override these with +file_column+'s +options+ parameter
DEFAULT_OPTIONS = {
- :root_path => File.join(RAILS_ROOT, "public"),
+ :root_path => File.join(Rails.root, "public"),
:web_root => "",
:mime_extensions => MIME_EXTENSIONS,
:extensions => EXTENSIONS,



(5)Gemfile修正

gem 'rmagick', :require => 'RMagick'

(6)bundle installを実行

bundle install

(7)scaffold雛形作成
rails g scaffold Entry name:string image:string
rake db:migrate

(8)model修正
app/models/entry.rb



class Entry < ActiveRecord::Base
file_column :image,
:root_path => "#{Rails.root}/public/store",
:web_root => "store/",
:magick => {
:versions => {
:thumb => { :crop => "1:1", :size => "50x50" },
:middle => "150x150>",
:large => "480x480>"
}
}
end

(9)viewを修正。

app/views/entries/_form.html.erb
multipart、file_column_fieldに変更。


<%= form_for(@entry, :html => { :multipart => true }) do |f| %>
<% if @entry.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@entry.errors.count, "error") %> prohibited this entry from being saved:</h2>

<ul>
<% @entry.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :image %><br />
<%= file_column_field "entry", "image" %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>


(10)view修正2
app/views/entries/show.html.erb
image_tagに変更。


<p id="notice"><%= notice %></p>

<p>
<b>Name:</b>
<%= @entry.name %>
</p>

<p>
<b>Image:</b>
<%= image_tag(url_for_image_column(@entry, "image", :middle)) if @entry.image %>
</p>


<%= link_to 'Edit', edit_entry_path(@entry) %> |
<%= link_to 'Back', entries_path %>


(10)view修正3
app/views/entries/index.html.erb
image_tagに変更。

<p id="notice"><%= notice %></p>

<p>
<b>Name:</b>
<%= @entry.name %>
</p>

<p>
<b>Image:</b>
<%= image_tag(url_for_image_column(@entry, "image", :middle)) if @entry.image %>
</p>


<%= link_to 'Edit', edit_entry_path(@entry) %> |
<%= link_to 'Back', entries_path %>



(11)サーバ起動
rails s

<関連エントリ>
篳篥日記 [Rails] ruby 1.9.2 + Rails 3.0 + file_column
http://d.hatena.ne.jp/hichiriki/20100927

0 件のコメント: