Delve into the process of enhancing a Rails app with image upload functionality and transitioning to a PostgreSQL database for performance and compatibility with platforms like Heroku. This comprehensive tutorial provides a step-by-step guide on implementing image upload via Active Storage and migrating from SQLite to PostgreSQL.
Key Insights
- The tutorial covers adding image uploading capabilities to a Rails application using Active Storage, a feature of Rails that helps with uploading files to services like Amazon S3.
- Images are added to the 'cookbook' app; users can upload images for each recipe, and these images are stored using Amazon S3.
- To prepare the Rails app for real-world deployment, the tutorial guides users in migrating from an SQLite database to PostgreSQL.
- PostgreSQL is a robust open-source database system that can handle significant traffic, making it a suitable choice for applications deployed to the public.
- The tutorial details the process of installing and setting up PostgreSQL, including the creation of a new user and database for the Rails application.
- Useful PostgreSQL commands are provided, which can assist in managing databases and user roles within the PostgreSQL environment.
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.
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
Return to your cookbook app. Open in your code editor of choice.
We’ll need the Terminal open in the cookbook directory as well. By now you should be getting used to this!
-
Open Gemfile, scroll to around line 26 and uncomment the following:
gem 'image_processing', '~> 1.2'
Save the file.
-
Switch back to Terminal and type:
bundle
-
Let’s initialize Active Storage! In your Terminal window, run the following commands:
rails active_storage:install rails db:migrate
-
Edit the recipe model to add the attachment:
class Recipe < ApplicationRecord has_one_attached :image end
-
Let’s get the rails server running in another Terminal window:
rails s
-
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 %>
-
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
-
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>
-
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>
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:
- MySQL
- MariaDB
- Microsoft SQL Server
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
-
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
-
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
-
You should see some output including:
==> Successfully started `PostgreSQL` (label: homebrew.mxcl.PostgreSQL)
-
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
-
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.
-
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 | {}
-
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.
-
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;
Run
\du
again to see that the privilege has been added.Let’s switch to our cookbook user. Type
\q
to quit the PostgreSQL console.Back at the Terminal, type
psql PostgreSQL -U cookbook
to sign in as the cookbook user.Back in the PostgreSQL prompt, run the SQL command
CREATE DATABASE cookbook;
to create the cookbook database.-
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!
Type
\q
to leave the PostgreSQL console.-
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'
-
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'
Run
bundle
to install the new gem and remove the deleted one.-
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
Run
rails db:migrate
in Terminal. You should see the CreateRecipes and CreateActiveStorageTables migrations run successfully.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.