Carrierwave Gem for file uploads in Rails

If we are building a web application, one of the features we would definitely want is the image uploading feature. In fact, image uploading is one important feature that the modern-day applications can simply not be imagined without. And not just that, it is even more important considering the fact that images uploading also plays a role in search engine optimization. CarrierWave gem can be integrated with our Rails applications and it provides a simple and extremely flexible way to upload files.

Rails Application Setup:

To set this up in our Rails Application, after creating a new project, we need to add 2 gems in the gem file. The first is CarrierWave itself, and the second is the bootstrap-sass gem(optional).
gem 'carrierwave', '~> 0.9'
gem 'bootstrap-sass', '~> 2.3.2'

Installation:

We run bundle install and then create our models and controllers for our application. bundle install
rails g model Modelname field:datatype =>attachment:string
rake db:migrate
rails g controller controllername action => index new create destroy Now, we need to create an uploader. The uploaders tell carrierwave how to handle the file once it’s uploaded => rails g uploader attachment It will create a folder named uploaders in the app folder with a file attachment_uploader.rb.

How you play with it:

app/uploaders/attachment_uploader.rb
class  Attachment Uploader < CarrierWave::Uploader::Base
   storage :file
   def store_dir
     "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
   end
 end
Now in our resumes controller and add some code to handle form processing:
class ResumesController < ApplicationController
  def index
    @resumes = Resume.all
  end

  def new
    @resume = Resume.new
  end

  def create
    @resume = Resume.new(resume_params)

    if @resume.save
      redirect_to resumes_path, notice: "The resume #{@resume.name} has been uploaded."
    else
      render "new"
    end
  end

  def destroy
    @resume = Resume.find(params[:id])
    @resume.destroy
    redirect_to resumes_path, notice:  "The resume #{@resume.name} has been deleted."
  end

private
  def resume_params
    params.require(:resume).permit(:name, :attachment)
  end
end
Now we need to set up our view files:
For new.html.erb
<%= form_for @resume, html: { multipart: true } do |f| %> <%= f.label :name %> <%= f.text_field :name %> <%= f.label :attachment %> <%= f.file_field :attachment %> <%= f.submit “Save”, class: “btn btn-primary” %> <% end %>
 
For index.html.erb
<% @resumes.each do |resume| %> <% end %>
Name Download Link
<%= resume.name %> <%= link_to “Download Resume”, resume.attachment_url %> <%= button_to “Delete”, resume, method: :delete, class: “”, confirm: “Are you sure that you want to delete #{resume.name}?” %>
 
app/uploaders/attachment_uploader.rb
class AttachmentUploader < CarrierWave::Uploader::Base
  storage :file

  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  def extension_white_list
    %w(pdf doc htm html docx)
  end
end

References:

Github: https://github.com/carrierwaveuploader/carrierwave RubyGems: https://rubygems.org/gems/carrierwave/versions/0.11.2 Railscasts: http://railscasts.com/episodes/253-carrierwave-file-uploads

Subscribe For Latest Updates

Related Posts

About Post Author

Leave a Comment

Your email address will not be published. Required fields are marked *