plugin in rails · RubyonRails

Acts_as_attachment plugin in Rails

MainURl :

Reference : –> Tutorial

d:>rails attach

d:\attach>ruby script/plugin source

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

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

Open the rb File d:\attach\app\dvd_covers_controller.rb

class DvdCoversController < ApplicationController
def index
@dvd_covers = DvdCover.find(:all)

def new
@dvd_cover =

def show
@dvd_cover = DvdCover.find params[:id]

def create
@dvd_cover = DvdCover.create! params[:dvd_cover]
redirect_to :action => ‘show’, :id => @dvd_cover
rescue ActiveRecord::RecordInvalid
render :action => ‘new’

Open the rhtml File d:\attach\app\views\dvd_covers\index.rhtml

<h1>DVD Covers</h1>

<% @dvd_covers.each do |dvd_cover| -%>
<li><%= link_to dvd_cover.filename, :action => ‘show’, :id => dvd_cover %></li>
<% end -%>

<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


main URL:


acts_as_attachment :storage => :file_system, :thumbnails => { :normal => ‘300>’, :thumb => ’75’ }


<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’)


3 thoughts on “Acts_as_attachment plugin in Rails

  1. Greetings I am so delighted I found your webpage, I really found you by accident, while I was searching on Digg for something else,
    Nonetheless I am here now and would just like to say thank you for
    a fantastic post and a all round enjoyable blog (I also
    love the theme/design), I don’t have time to read through it all at the minute but I have book-marked it and also included your RSS feeds, so when I have time I will be back to read much more, Please do keep up the great job.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s