Get started developing Ruby on Rails apps that run on the App Engine flexible environment. The apps you create run on the same infrastructure that powers all of Google's products, so you can be confident that they can scale to serve all of your users, whether there are a few or millions of them.
This tutorial assumes you are familiar with Rails web development. It walks you through setting up Cloud SQL for PostgreSQL with a new Rails app. You can also use this tutorial as a reference for configuring existing Rails apps to use Cloud SQL for PostgreSQL.
This tutorial supports and requires Ruby 3.0 or later.
Before you begin
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Cloud SQL Admin API.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init -
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Cloud SQL Admin API.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
Preparing a Cloud SQL for PostgreSQL instance
Set up a Cloud SQL for PostgreSQL instance for this tutorial.
Create a PostgreSQL instance. In this tutorial, the name of the instance is
rails-cloudsql-instance.Create a database in the instance. In this tutorial, the name of the production database is
cat_list_production.
Setting up your local environment for Rails
To set up your local environment for this tutorial:
For additional information on installing Rails and its dependencies, see the official Getting started with Rails guide.
After you complete the prerequisites, create and deploy a Rails app by using Cloud SQL for PostgreSQL. The following sections guide you through configuring, connecting to Cloud SQL for PostgreSQL, and deploying an app.
Create a new app to list cats
Run the
rails newcommand to create a new Rails app. This app stores a list of cats in Cloud SQL for PostgreSQL.rails new cat_sample_appGo to the directory that contains the generated Rails app.
cd cat_sample_app
Run the app locally
To run the new Rails app on your local computer:
Start a local web server:
bundle exec bin/rails serverIn a browser, go to http://localhost:3000/
The sample app displays the Rails logo with the Rails and Ruby versions.
Generate scaffolding for a list of cats
Generate scaffolding for a resource named Cat that is used to form a list
of cats with their name and age.
Generate the scaffolding.
bundle exec rails generate scaffold Cat name:string age:integerThe command generates a model, controller, and views for the
Catresource.invoke active_record create db/migrate/20230922063608_create_cats.rb create app/models/cat.rb invoke test_unit create test/models/cat_test.rb create test/fixtures/cats.yml invoke resource_route route resources :cats invoke scaffold_controller create app/controllers/cats_controller.rb invoke erb create app/views/cats create app/views/cats/index.html.erb create app/views/cats/edit.html.erb create app/views/cats/show.html.erb create app/views/cats/new.html.erb create app/views/cats/_form.html.erb create app/views/cats/_cat.html.erb invoke resource_route invoke test_unit create test/controllers/cats_controller_test.rb create test/system/cats_test.rb invoke helper create app/helpers/cats_helper.rb invoke test_unit invoke jbuilder create app/views/cats/index.json.jbuilder create app/views/cats/show.json.jbuilder create app/views/cats/_cat.json.jbuilderOpen the file
config/routes.rbto see the following generated content.