Getting ready to Deploy

Free Ruby on Rails Tutorial

Discover how to add an image to your cookbook app and explore the powerful PostgreSQL database in this Ruby on Rails tutorial.

This exercise is excerpted from Noble Desktop’s past web development training materials. Noble Desktop now teaches JavaScript and the MERN Stack in our Full Stack Development Certificate. To learn current skills in web development, check out our coding bootcamps in NYC and live online.

Topics Covered in This Ruby on Rails Tutorial:

Adding an Image to the Cookbook, Introducing PostgreSQL

Exercise Overview

In the first fourteen sections, we’ve covered almost every topic under the sun! But everything we’ve done so far has been done locally, on our own computers, where only we can see it. Now we’re going to practice sending our applications out into the world, where anyone can access them from anywhere.

This section is about getting ready for that big step. We’re going to return to the cookbook app we created way back at the beginning of this class. Using what we now know about Active Storage, we’re going to add a picture to each recipe so we can play with uploading images to Amazon S3.

Full-Stack Web Development Certificate: Live & Hands-on, In NYC or Online, 0% Financing, 1-on-1 Mentoring, Free Retake, Job Prep. Named a Top Bootcamp by Forbes, Fortune, & Time Out. Noble Desktop. Learn More.

Then, we’re going to install the PostgreSQL database (often referred to as just PostgreSQL) and its associated gem and explore using a production database.

Adding an Image to the Cookbook

  1. Return to your cookbook app. Open in your code editor of choice.

  2. We’ll need the Terminal open in the cookbook directory as well. By now you should be getting used to this!

  3. Open Gemfile, scroll to around line 26 and uncomment the following:

    gem 'image_processing', '~> 1.2'
  4. Save the file.

  5. Switch back to Terminal and type:

    bundle
  6. Let’s initialize Active Storage! In your Terminal window, run the following commands:

    rails active_storage:install
    rails db:migrate
  7. Edit the recipe model to add the attachment:

    class Recipe < ApplicationRecord
      has_one_attached :image
        end
  8. Let’s get the rails server running in another Terminal window:

    rails s
  9. Edit app > views > recipes > _form.html.erb and add the file upload field:

    <%= form.text_area :directions %>
      </div>
    
      <div class="field">
        <%= form.label :image %>
        <%= form.file_field :image %>
      </div>
    
      <div class="actions">
        <%= form.submit %>
  10. Edit app > controllers > recipes_controller.rb to add support for the image upload:

    def recipe_params
            params.require(:recipe).permit(:title, :description, :prep_time, :ingredients, :directions,  :image)
    end
  11. Edit app > views > recipes > show.html.erb and add the image:

    <%= @recipe.title %>
    </h1>
    
    <% if @recipe.image.attached? %>
      <p>
        <%= image_tag(@recipe.image.variant(resize: "400x400")) if @recipe.image.attached? %>
      </p>
    <% end %>
    
    <p>
      <em><%= @recipe.description %></em>
  12. And, finally, let’s add a small version of the image to our index page. Edit app > views > recipes > index.html.erb:

    <thead>
        <tr>
          <th>Image</th>
          <th>Title</th>
          <th>Prep time</th>
    <% @recipes.each do |recipe| %>
          <tr>
            <td><%= image_tag(recipe.image.variant(resize: "100x100")) if recipe.image.attached? %></td>
            <td><%= recipe.title %></td>
  13. Go to http://localhost:,000 in your browser. Edit our existing recipe and add the pasta.jpg image from snippets. Save the page and check it out! Looking good.

Introducing PostgreSQL

Up to this point, we’ve been working exclusively in the SQLite database that comes with Rails. SQLite is convenient because it’s small, simple to use, and simple to set up on a local environment. What SQLite is not good at is handling any amount of traffic at all (hence the “lite” appellation). When we send our application out into the real-world, we need a solid, reliable database that will take care of our data and hold up well under strain.

There are plenty of options out there in the real-world. Some popular ones include:

And if one of these is already your preferred database, rejoice! Rails has support for all of them. But the most popular database option for Rails is PostgreSQL. Like MySQL, PostgreSQL is an open source database with a long history of solid performance. It’s also the database-of-choice for the Heroku hosting environment, so if we’re going to host our Rails app on Heroku, we need to get acquainted with PostgreSQL.

Installing PostgreSQL

  1. First of all, we need to ensure PostgreSQL is installed on our local machine. This is important so we can import our database backups from Heroku later. On a Mac, we can install it easily using Homebrew:

    brew install PostgreSQL
  2. Wait while that chews through what it’s got to chew through. When it finishes, we should have a shiny new postgress install on our Mac. Next, let’s start it up, and tell it to start up automatically when we restart our computer:

    pg_ctl -D /usr/local/var/PostgreSQL start && brew services start PostgreSQL
  3. You should see some output including:

    ==> Successfully started `PostgreSQL` (label: homebrew.mxcl.PostgreSQL)
  4. This is a production database, so it’s going to require a little more setup than SQLite. To start the process, we need to open a console to PostgreSQL and register a new user for ourselves. We can do that with the command:

    psql PostgreSQL
  5. You should now see the following:

    psql (11.4)
    Type "help" for help.
    
    PostgreSQL=#

    This is our PostgreSQL prompt. We can type PostgreSQL commands here.

  6. Our first command will be to see which users exist on the system. In PostgreSQL parlance, this command is \du:

    PostgreSQL=# \du
                                   List of roles
     Role name |                         Attributes                         | Member of—————-+——————————————————————————————+—————-
     matt      | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
  7. Looks it’s like it’s just me so far. It’s all well and good to be a Superuser, but for our Rails app, we’d be safer creating an account just for it (as opposed to one that can administer our whole PostgreSQL install!) We can do that by entering the following command:

    CREATE ROLE cookbook WITH LOGIN PASSWORD 'booksRcooked';

    What we would normally call a username, PostgreSQL calls a Role. Feel free to replace the role name cookbook and password booksRcooked with your own, hopefully less absurd, values.

  8. Type \du to see your new user. They’re there, but they don’t have any privileges! Let’s hook them up with the ability to create a database. (They’ll need this to work effectively with Rails.)

    ALTER ROLE cookbook CREATEDB;
  9. Run \du again to see that the privilege has been added.

  10. Let’s switch to our cookbook user. Type \q to quit the PostgreSQL console.

  11. Back at the Terminal, type psql PostgreSQL -U cookbook to sign in as the cookbook user.

  12. Back in the PostgreSQL prompt, run the SQL command CREATE DATABASE cookbook; to create the cookbook database.

  13. Run the PostgreSQL command \list to see a list of all databases. The output will be something like this:

    PostgreSQL=> \list
                           List of databases
       Name    |  Owner   | Encoding | Collate | Ctype | Access privileges—————-+—————+—————+————-+———-+—————————-
     cookbook  | cookbook | UTF8     | C       | C     | 
     PostgreSQL  | matt     | UTF8     | C       | C     | 
     template0 | matt     | UTF8     | C       | C     | =c/matt          +
           |          |          |         |       | matt=CTc/matt
     template1 | matt     | UTF8     | C       | C     | =c/matt          +
           |          |          |         |       | matt=CTc/matt
    (4 rows)

    There’s our database!

  14. Type \q to leave the PostgreSQL console.

  15. Now that PostgreSQL is installed and configured, we can switch to using it for development purposes. Edit your Gemfile and add to the bottom of the file:

    gem 'pg'
  16. Now that we have the new database gem in our Gemfile, we have to remove sqlite3, because Heroku will not allow us to deploy an app that has it in the Gemfile. In your Gemfile, around lines 8 and 9, find the following code and delete it (commenting it out is insufficient).

    # Use sqlite3 as the database for Active Record
    gem 'sqlite3', '~> 1.4'
  17. Run bundle to install the new gem and remove the deleted one.

  18. Now we need to set up our Rails database config. Edit config > database.yml and change it as follows:

    default: &default
      adapter: PostgreSQL
      encoding: unicode
      pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
      timeout:,000
    
    development:
      <<: default
      database: cookbook
      username: cookbook
      password: booksRcooked
      host: localhost
  19. Run rails db:migrate in Terminal. You should see the CreateRecipes and CreateActiveStorageTables migrations run successfully.

  20. If you visit http://localhost:,000 in your browser, the site still works, but it’s empty. Switching databases destroyed our existing data. We’ll add a new recipe once we get to Heroku, and test syncing it to our local. We’re done for now!

Useful PostgreSQL Commands

  • —List all databases.
  • [databasename]—Switch active database.
  • List tables for the active database.
  • —Shows information for a specific table.
  • List all users.
  • Quit psql.

Noble Desktop Publishing Team

The Noble Desktop Publishing Team includes writers, editors, instructors, and industry experts who collaborate to publish up-to-date content on today's top skills and software. From career guides to software tutorials to introductory video courses, Noble aims to produce relevant learning resources for people interested in coding, design, data, marketing, and other in-demand professions.

More articles by Noble Desktop Publishing Team

How to Learn Coding

Master Coding with Hands-on Training. Learning How to Code in JavaScript, Python, and Other Popular Languages Can Pave the Way to a Job in Tech Such As Web Development, Data Science & Analytics, or Software Engineering.

Yelp Facebook LinkedIn YouTube Twitter Instagram