Advertisement
  1. Code
  2. Ruby
Code

Rails Image Upload Using Dragonfly

Difficulty:BeginnerLength:MediumLanguages:

File uploading is an important feature in web applications. Aside from enabling users to upload profile pictures, the use of file uploading features varies. I have shown you how to enable file uploading in your Rails application using different gems. Today I will be showing you how to do the same using Dragonfly.

Dragonfly is a highly customizable Ruby gem for handling images and other attachments and is already in use on thousands of websites.

You may be given a task to enable file uploading in a Rails application and may not want to make use of the other gems that are out there. You can give Dragonfly a shot, and you will definitely not regret it.

In this tutorial you will create a simple Rails application; I named mine Dragon-Uploader. The application will have just one feature: image uploading.

Installing ImageMagick

To use dragonfly, you need ImageMagick installed on your machine. Follow any of the steps below, depending on your operating system.

Mac Users:

brew install imagemagick

Ubuntu users:

sudo apt-get install imagemagick

Rails Application Generation

rails new dragon-uploader -T

The -T option ensures that your Rails application is generated without the default testing suite.

Go to your Gemfile and add the dragonfly gem.

Do not forget to bundle.

bundle install

Let's generate our controller.

rails generate controller Photos

Integrating Dragonfly

The first step to integrating Dragonfly into your Rails application is to run the dragonfly generation command from your terminal.

rails generate dragonfly

This will create an initializer file for Dragonfly in your config/initializers folder.

The file looks like this:

rails generate model Photo

Dragonfly provides an accessor that you will need to add to your model. With this you can read and write images.

Now navigate to your migration file and add columns.

Note: If you are making use of avatar and not image as I did above, you should change the column to avatar_uid.

Migrate your database:

rake db:migrate

Set up your PhotosController with the necessary actions to upload an image. It should look like this:

You will need to configure your routes.

For now, add routes to the three actions you have created.

You need to set up your views as I have below:

We will come back to these views later.

Validations

For security purposes, you do not want to grant your users the privilege of uploading files of any type. Dragonfly provides you with the necessary methods for this in your initializers.

Now edit your photo model to look like what I have below:

Here is a full list of the validations Dragonfly offers:

You can read more about it in the Dragonfly documentation.

You should also consider giving your users the option to edit their saved images. To do this, we need to add two action methods to our PhotosController and create an edit page in our views. You might want to add the delete and show action while you're at it, as I have below:

If you try to access the show or edit page, you will be presented with errors. This is because we restricted the route to :new, :index, and :update. Now go ahead and change that; it should look like this:

Conclusion

At this point, you can now integrate Dragonfly into your Rails application. Be sure to check out the documentation if you want to try more features not mentioned here. I hope you enjoyed it.

Remember, you can always add feedback, questions, and comments in the form below.

Advertisement
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.