For image uploading, rails3 having plugins and gems. But here we are going to discuss on how to upload and remove images through rails3 without using gems or plugins.
Here I am going to discuss on how to upload image using rails3 application. There are many gems and plugins available for this. But here we will do it without using any gem or plugin.
First we have to create a table named as “images” . Please find the following SQL query to generate that table
CREATE TABLE IF NOT EXISTS `images` ( `id` int(11) NOT NULL AUTO_INCREMENT, `image_name` varchar(255) NOT NULL, `image_type` varchar(255) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
Next for images table we need to create image model(app/models/image.rb). Place the following code in the image.rb file.
class Image < ActiveRecord::Base # create a virtual uploaded_image field in the class. attr_accessor :uploaded_image attr_accessible :image_name, :image_type end
We have taken a virtual field which we will use it while uploading image, before that we will have to define the routes in ‘config\routes.rb’. please place the following code in routes.rb file.
# it means we have public controller having upload_image action resources :public, :only => [:upload_image] # define the routes match '/uploadimage', :to => 'public#upload_image'
Now as per the routes we have to create a controller named as PublicController. So create a public_controller.rb in “app\controllers\”. Then place the following code in it.
class PublicController < ApplicationController def upload_image # get all the images from the db @images = Image.all # creating an object for the Image class @image = Image.new end end
In the action page ‘app\views\public\upload_image.html.erb’, place the following code
{:controller => "public",:action => "uploading",:method => :post}, :html => {:multipart => true} do %>
0 %> '140px', :width => '185px' %>
Remove No images found
Here the form action is uploading so we have to create another action in PublicController. So add the following code to the PublicController.
def uploading # creating an object for the Image class @image = Image.new # get the file_type that have been uploaded file_type = params[:image][:uploaded_image].content_type # check the file type for images if file_type == 'image/jpeg' || file_type == 'image/jpg' || file_type == 'image/png' || file_type == 'image/gif' || file_type == 'image/bmp' # as per the file type give the images name case file_type when "image/jpeg" file_name = "pic_#{Time.now.strftime("%Y%m%d%H%M%S")}.jpg" when "image/png" file_name = "pic_#{Time.now.strftime("%Y%m%d%H%M%S")}.png" when "image/gif" file_name = "pic_#{Time.now.strftime("%Y%m%d%H%M%S")}.gif" when "image/bmp" file_name = "pic_#{Time.now.strftime("%Y%m%d%H%M%S")}.bmp" end # give the file path for image file_path = File.join(Rails.root, 'public', 'images', 'upload_images', file_name) # copy the image from the uploaded one to file_path File.open(file_path, 'wb') do |f| f.write params[:image][:uploaded_image].read end # @image.image_name = file_name @image.image_type = file_type # save the image @image.save #redirect to /uploadimage redirect_to uploadimage_path else # get all the images from the database @images = Image.all # add an error @image.errors.add(:image, "should be in jpeg/bmp/png/gif format") render 'upload_image' end end
For showing the error message in the time of uploading, we have placed “” code in the ‘app\views\public\upload_image.html.erb’. So we need to create “_error_img_messages.html.erb” file in “app\views\shared\”. Then place the following code in it.
prohibited the uploading process…
There were problems with the following fields:
In the ‘app\views\public\upload_image.html.erb’, we have shown a remove link against to each uploaded image. For this we need to create another action in PublicController. Please add the following code to PublicController.
def remove_image #get the image from db by image id image = Image.find(params[:id]) # get the image path img_path = File.join(Rails.root, 'public', 'images', 'upload_images', image.image_name) # delete the image file File.delete(img_path) # delete record from the database image.destroy redirect_to uploadimage_path end
After that we have to modify the routes.rb with the following code.
resources :public, :only => [:upload_image, :uploading, :remove_image] # define the routes match '/uploadimage', :to => 'public#upload_image' match '/uploadingimage', :to => 'public#uploading' match '/removeimage/:id', :to => 'public#remove_image'
Now your application is ready to upload and remove image using rails3.