Wednesday, February 3, 2010

Ruby on Rails File Column plugin usage

After installing the plugin, you should observe the below steps
In model
class Product < ActiveRecord::Base
file_column :image #image is the field name in db for Products table
end

Generate browse button in view

<%= file_column_field “product”, “image” %>

and display uploaded images in your view:

<%= image_tag url_for_file_column(“product”, “image”) %>

However, you may want to protect against the model object having no uploaded image:

<%= image_tag url_for_file_column(“product”, “image”) if @product.image %>

To resize every uploaded image to a maximum size of 640×480, you just have to declare an additional option.

class Product < ActiveRecord::Base
file_column :image, :magick => { :geometry => “640×480>” }
end

You can even automatically create versions in different sizes that have nice filenames…

class Product < ActiveRecord::Base
file_column :image, :magick => {
:versions => { “thumb” => “50×50″, “medium” => “640×480>” }
}
end

and display them in your view:

<%= image_tag url_for_file_column(“image”, “entry”) %>

No comments: