MainURl : http://weblog.techno-weenie.net/articles/acts_as_attachment
Reference : http://technoweenie.stikipad.com/plugins/show/Acts+as+Attachment –> Tutorial
d:>rails attach
d:\attach>ruby script/plugin source http://svn.techno-weenie.net/projects/plugins
d:\attach>ruby script/plugin install acts_as_attachment
d:\attach>ruby script/generate attachment_model dvd_cover
d:\attach>ruby script/generate controller dvd_covers index new show
OPEn the rb File d:\attach\db02_create_dvd_covers.rb
create_table :dvd_covers do |t|
t.column “dvd_id”, :integer
t.column “content_type”, :string
t.column “filename”, :string
t.column “size”, :integer
t.column “parent_id”, :integer
t.column “thumbnail”, :string
t.column “width”, :integer
t.column “height”, :integer
end
Open the rb File d:\attach\app\model\dvd_cover.rb
class DvdCover < ActiveRecord::Base
belongs_to :dvd
acts_as_attachment :storage => :file_system, :max_size => 300.kilobytes, :content_type => :image
validates_as_attachment
end
Open the rb File d:\attach\app\dvd_covers_controller.rb
class DvdCoversController < ApplicationController
def index
@dvd_covers = DvdCover.find(:all)
end
def new
@dvd_cover = DvdCover.new
end
def show
@dvd_cover = DvdCover.find params[:id]
end
def create
@dvd_cover = DvdCover.create! params[:dvd_cover]
redirect_to :action => ‘show’, :id => @dvd_cover
rescue ActiveRecord::RecordInvalid
render :action => ‘new’
end
end
Open the rhtml File d:\attach\app\views\dvd_covers\index.rhtml
<h1>DVD Covers</h1>
<ul>
<% @dvd_covers.each do |dvd_cover| -%>
<li><%= link_to dvd_cover.filename, :action => ‘show’, :id => dvd_cover %></li>
<% end -%>
</ul>
<p><%= link_to ‘New’, :action => ‘new’ %></p>
Open the rhtml File d:\attach\app\views\dvd_covers\new.rhtml
<h1>New DVD Cover</h1>
<% form_for :dvd_cover, :url => { :action => ‘create’ }, :html => { :multipart => true } do |f| -%>
<p><%= f.file_field :uploaded_data %></p>
<p><%= submit_tag :Create %></p>
<% end -%>
Open the rhtml File d:\attach\app\views\dvd_covers\show.rhtml
<p><%= @dvd_cover.filename %></p>
<%= image_tag @dvd_cover.public_filename, :size => @dvd_cover.image_size %>
<%= link_to ‘Back’, :action => ‘new’ %>
d:\attach>rake db:migrate
now goto this url http://localhost:3000/dvd_covers in ur browser
THUMBNAIL:
main URL: http://weblog.techno-weenie.net/articles/acts_as_attachment/thumbnailing
MODEl :
acts_as_attachment :storage => :file_system, :thumbnails => { :normal => ‘300>’, :thumb => ’75’ }
SHOW PAGE:
<p>Original: <%= link_to @dvd_cover.filename, @dvd_cover.public_filename %></p>
<% @dvd_cover.thumbnails.each do |thumb| -%>
<p><%= thumb.thumbnail.to_s.humanize %>: <%= link_to thumb.filename, thumb.public_filename %></p>
<% end -%>
<% DvdCover.attachment_options[:thumbnails].keys.each do |key| -%>
<p><%= key.to_s.humanize %>: <%= link_to key, @dvd_cover.public_filename(key) %></p>
<% end -%>
Controller :
def index
@dvd_covers = DvdCover.find(:all, :conditions => ‘parent_id is null’)
end