Add picture uploads
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.
Gemfile file in your Text Editor. Below the following line:
add this line to the file and save it:
Open the Terminal app and press Ctrl+C to quit the Rails server.
Then run the following command in the Terminal:
This will install the “carrierwave” gem we added to the
Explain what libraries (Ruby gems) are and why they are useful. Describe what Open Source software is.
Generating a picture uploader
We can now generate the code for handling file uploads. In the Terminal run the following command:
If an error is shown that the uploader cannot be found also add the following line:
If you added this gem, please run this command in the Terminal app to install the missing gem, and try again:
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.
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:
add this line and save the file:
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.
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.
app/views/ideas/_form.html.erb file in your Text Editor and change the following line:
to this line and save it:
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:
to this line and save the file:
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.
.gitignore file add add this line at the bottom:
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.
- Guide 1: Start of the guide
- Guide 2: Get to know the tools
- Guide 3: Guide to install Rails
- Guide 4: Build Your First App
- Guide 5: Style your app using HTML and CSS
- Guide 6: Add a new page to your app
- Guide 7: Add a new homepage to your app
- Guide 8: Add picture uploads (Current page!)
- Guide 9: Push Your App to GitHub
- Guide 10: Put your app online with one of these services:
- Guide 11: Style the idea pages using HTML and CSS
- Guide 12: Add comments to your app
- Guide 13: Create picture thumbnails
- Guide 14: Test your app with RSpec
View all guides