SUPPORT THE SITE WITH A CLICK

Subscribe Rss:

SUPPORT THE SITE WITH A CLICK

Tuesday, May 20, 2008

Using attachment_fu to upload(add) image to your Rails application

Hi,i am new to rails application,i had some task in my application such as to upload image.so i was searching in google,how to upload,i got some answers.But that not seems to good as i liked.Then i got this plugin attachment_fu.This plugin seems to pretty good for me.



Features


attachment-fu
=====================

attachment_fu is a plugin by Rick Olson (aka technoweenie ) and is the successor to acts_as_attachment.

attachment_fu functionality
===========================

attachment_fu facilitates file uploads in Ruby on Rails. There are a few storage options for the actual file data, but the plugin always at a minimum stores metadata for each file in the database.

There are three storage options for files uploaded through attachment_fu:
File system
Database file
Amazon S3

Each method of storage many options associated with it that will be covered in the following section. Something to note, however, is that the Amazon S3 storage requires you to modify config/amazon_s3.yml and the Database file storage requires an extra table.

Step 1:


Installing the plugin is as easy as it gets:



script/plugin install http://svn.techno-weenie.net/projects/plugins/attachment_fu/

Step 2:


Creating the scaffold


ruby script/generate scaffold image parent_id:integer content_type:string filename:string thumbnail:string size:integer height:integer

Step 3:


Let's start with the upload form in the new.html.erb file:



new.html.erb
<h1>New Image</h1>
<%= error_messages_for :image %>

<%= render:partial=>'form',:locals=>{:button=>'Create'}%>

<%= link_to 'Back', images_path %>

-----------------------------------------------------------------
_form.html.erb

<% form_for(@image,:html => { :multipart => true }) do |f| %>

<%= f.file_field :uploaded_data, :class => 'input-file' %>


<p>
<%= f.submit button %>
</ph1>
<% end %>

Step 4:


Write a Controller.
The controller is oblivious to the fact that we're uploading images. The new action displays the upload form and the create action accepts the POST data.




As we have created the scaffold controller code will be generated by default:

def create
@image = Image.new(params[:image])

respond_to do |format|
if @image.save
flash[:notice] = 'Image was successfully created.'
format.html { redirect_to(@image) }
format.xml { render :xml => @image, :status => :created, :location => @image }
else
format.html { render :action => "new" }
format.xml { render :xml => @image.errors, :status => :unprocessable_entity }
end
end
end


Step 5 :


Next we need a Image model to store the uploaded file information.


Migration file

class CreateImages < ActiveRecord::Migration
def self.up
create_table :images do |t|
t.integer :parent_id
t.string :content_type
t.string :filename
t.string :thumbnail
t.integer :size
t.integer :height


end
end

def self.down
drop_table :images
end
end


Step 6:


In model we have to write these lines


class Image < ActiveRecord::Base
<--start--->
has_attachment :content_type => :image,
:storage => :file_system,
:max_size => 500.kilobytes,
:resize_to => '320x200>',
:thumbnails => { :thumb => '100x100>' }

validates_as_attachment
<--end-->
end






Options available in attachment_fu are:


  • :content_type - The content types that are allowed, which defaults to all content types. Using :image allows all standard image types.

  • :min_size - The minimum size allowed, which defaults to 1 byte

  • :max_size - The maximum size allowed, which defaults to 1 megabyte

  • :size - A range of allowed sizes, which overrides the :min_size and :max_size options

  • :resize_to - An array of width/height values, or a geometry string for resizing the image

  • :thumbnails - A set of thumbnails to generate, specified by a hash of filename suffixes and resizing options. This option can be omitted if you don't need thumbnails, and you can generate more than one thumbnail simply by adding names and sizes to the hash.

  • :thumbnail_class - Sets what class (model) to use for thumbnails, which defaults to the current class (MugShot, in this example). You could, for example, use a different model class with a different set of validations.

  • :storage - Sets where the actual image data is stored. Options include :file_system, :db_file, and :s3.

  • :processor - Sets what image processor to use. Options include ImageScience, Rmagick, and MiniMagick. By default, it will use whatever you have installed.

  • :path_prefix - Path to store the uploaded files, which defaults to public/#{table_name} by default for the filesystem. If you're using the S3 backend, it defaults to just #{table_name}.



To know briefly about the plugin and its options,you can view the README file of the plugin.
Path : --->vendor--->plugins--->attachment_fu--->README


Finally my customized show.html.erbwill look like this


<%= image_tag( @image.public_filename() ) %>
<%= link_to 'Edit', edit_image_path(@image) %> |
<%= link_to 'Back', images_path %>

For ur practice u can get the sample project

Note:This application has been created in rails 2.0.2.OperatinSystem-UbuntuLinux

No comments :

Post a Comment