Skip to main content

Simple Image and File Upload using Ruby on Rails (CarrierWave gem)




I’m going to show you a very simple way to upload your attachments or a picture using CarrierWave gem.

Steps:

1.    Open Terminal.

2.   Create a new project

terminal > rails new imageupload

3.   Add this line to Gemfile

    gem ‘carrierwave

4.   terminal > bundle install

5.   terminal > rails g uploader image

6.   terminal > rails g scaffold Painting name:string image:string

7.   terminal > rake db:migrate

8.   Add this line to painting.rb (app/models/painting.rb)

    class Painting < ActiveRecord::Base
    mount_uploader :image, ImageUploader
    end

9.   And then in your app/views/paintings/_form.html.erb edit your code, which become

    <%= f.label :image %>
    <%= f.text_field :image %>

   Change to

    <%= f.label :image %>
   <%= f.file_field :image %>

10.   edit your show.html.erb which become

    <%= image_tag @painting.image_url.to_s %>

11.   terminal > rails s

Note : Configure routes.rb for page redirection

Comments