Wednesday, February 11, 2009


Image uploading is easy in ruby on rails by using attachment_fu plugin

To install attachment_fu
ruby script/plugin install

Then u need to design table in migration like

here we will take user images
class CreateUserImages < ActiveRecord::Migration
def self.up
create_table "user_images",:force => true do |t|
t.column "user_id", :integer
t.column "primary", :boolean
t.column "created_at", :datetime

# Required attributes for attachment_fu plugin
t.column "filename", :string, :limit => 255
t.column "path", :string, :limit => 255
t.column "content_type", :string
t.column "size", :integer
t.column "width", :integer
t.column "height", :integer
t.column "parent_id", :integer
t.column "thumbnail", :string

def self.down
drop_table :user_images

In model we need to customize validations

class UserImage < ActiveRecord::Base
belongs_to :user

has_attachment :content_type => :image,
:storage => :file_system,
:path_prefix => 'public/website',
:size => 1.kilobyte..5.megabytes,
:resize_to => '279x116',
:thumbnails => {:thumb => '100x100>' }
validates_uniqueness_of :filename

def full_filename(thumbnail = nil)
file_system_path = (thumbnail ? thumbnail_class : self).attachment_options[:path_prefix].to_s
File.join(RAILS_ROOT, file_system_path, thumbnail_name_for(thumbnail))

And in User model

class User < ActiveRecord::Base
has_many :user_images

def primary_image

Here in controller only to display and create

class UserImagesController < ApplicationController
def new
@image =
respond_to do |format|

def create
# Required for Windows machines. Uncomment if you using Windows
# sleep(1)
user_data = {:user => current_user, :primary => current_user.images.empty? }

@image =[:image].merge(user_data))

respond_to do |format|
flash[:success] = "image successfully uploaded"
format.html { redirect_to(edit_user_path(current_user)) }
format.html { render :action => "new" }

And in new.rhtml

Current Primary Photo

<% if(@current_user.image.nil?) %>
<%= image_tag("default.png") %>
<% else %>
<%= image_tag(@current_user.primary_image.public_filename(:medium)) %>
<% end %>

Upload a new photo

<%= error_messages_for :image %>

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

Select a photo to upload

Photo: <%= f.file_field 'uploaded_data' %>
<%= submit_tag 'Upload Photo' %>
<% end %>

Follow the steps
Rake db:migrate
Then Script/server

If u want ur own validation to image then here are some key points
has_attachment(options = {})

* :size
o Range of sizes allowed.
o (1..1.megabyte) is the default. This overrides the :min_size and :max_size

* :resize_to
o Used by RMagick to resize images.
o Pass either an array of width/height, or a geometry string.

* :thumbnails
o Specifies a set of thumbnails to generate.
o This accepts a hash of filename suffixes and RMagick resizing options.
o This option need only be included if you want thumbnailing.
o :thumbnail_class
o Set which model class to use for thumbnails.o This current attachment class is used by default.

* :path_prefix
o path to store the uploaded files.
o path to store the uploaded files.
o Uses public/#{table_name} by default for the filesystem, and just #{table_name} for the S3 backend.
o Setting this sets the :storage to :file_system.

* :storage
o Specifies the storage system to use.
o Defaults to :db_file. Options are :file_system, :db_file, and :s3.

* :processor
o Sets the image processor to use for resizing of the attached image.
o Options include ImageScience, Rmagick, and MiniMagick. Default is whatever is installed.

* :content_type
o Allowed content types.
o Allows all by default. Use :image to allow all standard image types.

* :min_size
o Minimum size allowed.
o 1 byte is the default.

* :max_size
o Maximum size allowed.
o 1.megabyte is the default.

Reference link is:

File upload in rails

Follow the steps
Go to terminal and type
rails upload
cd upload
mkdir upload\public\data
ruby script/generate model DataFile

Then in data_file.rb
class DataFile < ActiveRecord::Base
name = upload['datafile'].original_filename
directory = "public/data"
# create the file path
path = File.join(directory, name)
# write the file, "wb") { |f| f.write(upload['datafile'].read) }

Then again in terminal
ruby script/generate controller Upload

class UploadController < ApplicationController
def index
render :file => 'app\views\upload\uploadfile.rhtml'
def uploadFile
post =[:upload])
render :text => "File has been uploaded successfully"


File Upload

<%= start_form_tag ({:action => 'uploadFile'},
:multipart => true) %>

<%= file_field 'upload', 'datafile' %>

<%= submit_tag "Upload" %>
<%= end_form_tag %>

Reference link:

Friday, October 17, 2008

Solution For "rewrite" error

If you get this error, and the error message is pointing you to a “link_to” call or something similar, then you may be using an instance variable that’s called ‘@url’ too.

I finally discovered that, in my case, I was using ‘@url’ in the controller for the view where I was making the ‘link_to’ call.

Long story short, if you see this error, comb through your code (models, controllers and views) for any variables that are called ‘@url’ and change them.


We should not use "url" word as controller name,model name,action name,attribute name ..
Apparently you can’t use “Class” as a class in Java either.

Yep, same as ‘private’, ‘public’, etc. With the slight difference that these are keywords at the language as oposed to framework level.

Then simply replace url word with other..

Friday, August 1, 2008

Ruby on Rails

Ruby on Rails is an open source web application framework for the Ruby programming language. It is often referred to as 'Rails' or 'RoR'. It is intended to be used with the Agile development methodology, which is often utilized by web developers for its suitability for short, client-driven projects.

What's in the Rails package?

Rails is a full-stack framework for developing database-backed web applications according to the Model-View-Control pattern. From the Ajax in the view, to the request and response in the controller, to the domain model wrapping the database, Rails gives you a pure-Ruby development environment. To go live, all you need to add is a database and a web server.