Upload Image in rails using “mongoid-paperclip” gem

I explained how to upload image in rails using “paperclip” in previous post: Upload image using paperclip in Rails. Now I’m going to explain how to upload image in rails using  “mongoid-paperclip” gem.

The ‘mongoid-paperclip’ gem is specially used in rails application with mongodb database where as the ‘paperclip’ gem is use in rails application with other than mongodb database like mysql, postgreSQL etc.

Before start this tutorial make sure that the ‘mongoid’ gem is already installed in your application. Lets follow following steps:

    1. Include the gem in your Gemfile
      gem "mongoid-paperclip", "~> 0.0.8", :require => "mongoid_paperclip"
      gem "aws-s3", :require => "aws/s3"
    2. Install gem (run on command prompt):
      bundle install
    3. Create controller (say Image ):
      rails g controller Images index new show
    4. Create model (say Image):
      rails g model Image
    5. Add following code in app/models/image.rb
      class Image
      include Mongoid::Document
      include Mongoid::Paperclip
      has_mongoid_attached_file :image
    6. Use following code to create form (in views/Images/new.html.erb)
      <%= form_for(:image, :url => {:action => 'create'},     :html => {:multipart => true}) do |f| %>
      <%= f.file_field :image %>
      <%= f.submit "Upload" %>
      <% end %>
    7. Use following code to Upload image (in ‘create’ action of  controllers/image_controller.rb)
      @image = Image.new(params[:image])
      if @image.save
      redirect_to :action => :show, :id => @image.id
    8. Use following code to view uploaded image (in ‘show’ action of controllers/image_controller.rb)
      @id = params[:id]
      @image = Image.find(@id)

      and add in (views/image/show.html.erb)
      <%= image_tag @image.image.url %>
    9. Start server and run on browser:

An error may occur while uploading image. To remove this error follow this link “gem core code”  and replace ur gem code (gems/mongoid-paperclip/lib/mongoid_paperclip.rb) and try it works fine.

8 thoughts on “Upload Image in rails using “mongoid-paperclip” gem

    but am getting a problem when i integrate this code with devise+mongo ID
    i have an USER model and for every user i have to upload an image..can u post a tutorial which has features DEVISE+MONGOID+PAPERCLIP

  2. @image.save is not working for me. It throws template missing error. And i gave like this

    if @image.save
    redirect_to :action => :show, :id => @image.id
    render “new”
    so now the template missing error has been solved. But its getting redirect to “new” .

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s