Add picture uploads

This guide is a part of the Rails Girls workshop main guides. Make sure you follow the numbered guides in order before continuing.

The ideas we added in the previous guide can benefit from a visual element, like a picture or drawing to spark the imagination. We can attach pictures by adding a file upload to the Idea model.

Installing a Ruby gem

To let us upload files in Rails app, we’ll need to install a piece of software in the app first.

Open the Gemfile file in your Text Editor. Below the following line:

gem "sqlite3"

add this line to the file and save it:

gem "carrierwave"

Open the Terminal app and press Ctrl+C to quit the Rails server.

Then run the following command in the Terminal:

bundle install

This will install the “carrierwave” gem we added to the Gemfile file.

Help from the coach

Explain what libraries (Ruby gems) are and why they are useful. Describe what Open Source software is.

Resources: RubyGems GitHub introduction and Wikipedia OSS

Generating a picture uploader

We can now generate the code for handling file uploads. In the Terminal run the following command:

rails generate uploader Picture

If an error is shown that the uploader cannot be found also add the following line:

gem "net-ssh"

If you added this gem, please run this command in the Terminal app to install the missing gem, and try again:

bundle install

Attaching the picture uploader to the idea model

Rails now knows about a way to upload pictures in your app. It needs a bit of help to understand where you want to attach these uploads to.

Open the app/models/idea.rb file in your Text Editor. This file is used to store your ideas in the database and fetch the ideas to show them. We’ll change it to tell Rails which field is a file upload.

Under the following line:

class Idea < ApplicationRecord

add this line and save the file:

mount_uploader :picture, PictureUploader

This mount_uploader line tells the Idea model that the picture field is a file upload. It will store information about the file upload on that field to display it later.

Uploading pictures

Now that your Idea model knows that the picture field is a file upload, we can change the form to create and edit pictures to select a picture.

Open the app/views/ideas/_form.html.erb file in your Text Editor and change the following line:

<%= form.text_field :picture %>

to this line and save it:

<%= form.file_field :picture %>

Run rails server.

In your browser open http://localhost:3000/ideas/new. Your “New idea” form will now show a different element on the page for the “Picture” field. Instead of a text field a file chooser is visible, recognizable by either a “Browse…” or “Choose File” button.

Fill in the form to create a new idea, but this time select a picture as well using this new element/button. Any random image you have on your laptop will do, it’s just a test.

Displaying the picture

You’ve now added a picture to your idea! You can’t see it yet after creating the idea, it will only show the filename right now. Let’s change it so it shows the picture.

To show the picture in the page of the idea itself, open the app/views/ideas/_idea.html.erb in the Text editor and change the following line:

<%= idea.picture %>

to this line and save the file:

<%= image_tag(idea.picture_url, width: 600) if idea.picture? %>

Using the image_tag we have told Rails to display the file upload as an image if it is present on the idea. Ideas without a picture will not show one.

Refresh the Browser. Your uploaded image should now be visible!

Configure Git to ignore uploads

By default your app saves all your images locally, which is fine during development, but we don’t want to save them with your app source code.

Open the .gitignore file add add this line at the bottom:

public/uploads/

If you’re ever stuck during a guide, please ask your coach for help and also consult this handy cheatsheet for Ruby, Rails, the console, the Text Editor etc.

Guides

View all guides