Can I Upload That?

Radiyah Sabur
5 min readApr 22, 2021

A Step-By-Step Guide to Uploading Files Using Active Storage within An Existing Rails App

Picture this, you are X amount of weeks into your coding bootcamp learning the magic of Ruby on Rails when you’re instructed to create a Rails application. You’ve come up with a killer idea for your app and are super stoked to start creating your masterpiece. The database is all set up so naturally, the next step would be to seed some data so you can test the functionality of your app while you code. You get to your users table and think to yourself, “I wonder if I can just upload pictures instead of typing out strings of image urls…?” The answer is yes, yes. 1,000 times YES!

I know you’re new to coding, and trying to do things that may not have been covered in lectures is a wee bit intimidating but, no need to worry, I got your back! In this guide, I’ll show you how to use Active Storage to upload those user images.

But first, what is Active Storage anyway?

It’s in.. The Cloud meme

Active Storage is another piece of magic that our beloved Rails has bestowed upon us. In a nutshell, it is a gem that gives you the ability to upload files (image files, PDFs, as well as videos), from your computer and into a cloud storage, either local or external, that can then be used in your rails application. It does this “through the use of built-in blob (binary large object) and Attachment models… Existing models do not need to be modified with additional columns to be associated with files. Active Storage uses polymorphic associations via the attachment join model, which connects to the actual blob.”Readme

Now that you have an idea of what Active Storage is, we’ll need to make sure your computer is running Rails 5.2 or higher. You can check this out by running rails -v in your terminal. If your version of Rails requires an update, go ahead and check out this upgrade process. Otherwise, let's get this party started! 🎉

STEP 1: Instal the Magic 🪄

To start, you’ll have to run the following code in your terminal:

$ rails active_storage:install

This line of code will generate a migration file that creates the tables necessary for Active Record to work. Go ahead and check the existence of the file in your app/db/migrate folder. If all checks out, the next thing you’ll want to do is run a migration using this code:

$ rails db:migrate

Once the migration is done, your db/schema.rb file should look a little something like the screenshot below.

After running those two lines of code, your schema.rb file should look something like this.

STEP 2: Setup the Magic 🪄

In this tutorial, we’ll be using local storage. But in order to do so, we’ll need to “declare Active Storage services” . We’ll do that by opening up our config/storage.yml file and adding this bit of code:

local:
service: Disk
root: <%= Rails.root.join("storage") %>

test:
service: Disk
root: <%= Rails.root.join("tmp/storage") %>

You’ll then need to ensure that your config/environments/development.rb file has the following code that tells Active Storage which service to use.

# Store uploaded files on the local file system (see config/storage.yml for options).config.active_storage.service = :local

Follow this guide if you’d like to, instead, use an external storage like Amazon S3, Google Cloud Storage, or Microsoft Azure Storage.

Step 3: Setup Associations & Form Input ↔️

Now, let’s make sure a user knows about it’s profile picture by setting up the correct associations. In this example, we are expecting a user to be able to have a single picture attached. We can do this by adding this line of code to our app/models/user.rb file:

has_one_attached :profile_pic

Yours should look something like this:

Make sure you user model has the right associations

To be able to use the upload functionality for your users, you’ll want to add some code to your new and edit existing users forms. There should be one form in your app/views/users/new.html.erb and another in your app/views/users/edit.html.erb files. Add the below code to your form_for to add the upload functionality to the forms:

<%= f.label "Profile pic: "%><%= f.file_field :profile_pic %>

In your browser, you should now see a button that can be used to upload images. Here’s an example of what it would look like with some CSS styling.

Example of an upload button within a create a new account form

Yay! You did it!

But wait, not so fast! We still can’t see the users image.

Step 4: Show the Images 🖼

The final step is to make the uploaded picture viewable for each user in the browser. In order to do that, lets add the following lines of code to our app/views/users/show.html.erb file:

<% if @user.profile_pic.attached? %><img src="<%= (url_for(@user.profile_pic)) %>" alt="<%= @user.name %>" width="500" height="400"><% end %>

This says, if there is a file attached, in this case a profile picture, create a url and pass it into the image tag using the user’s name as its alternative text along with some custom sizing.

And there you have it!

Ruby on Rails is truly the gift that keeps on giving 🎁. With so many built-in methods, it’s both simpler and time saving. I suggest checking out a few other guides to explore more options. Here are a few that helped me get comfortable with using Active Storage. They are here, here, here and here.

Thank you for taking the time to read my post. If you enjoyed it, please don’t forget to clap, comment and hit that follow button.

Happy coding!

--

--

Radiyah Sabur

Software Engineering Student at Flatiron School. I love Fashion, Film and all things sparkly.