開始開發在 App Engine 彈性環境上執行的 Ruby on Rails 應用程式。您所建立的應用程式,使用的正是所有 Google 產品採用的基礎架構,因此您可以放心,無論使用者只有幾人還是有數百萬人,應用程式都可調度資源,為所有使用者提供服務。
本教學課程假設您已熟悉 Rails 網路開發流程,並逐步引導您設定 PostgreSQL 適用的 Cloud SQL 和一個新的 Rails 應用程式。您也可以使用本教學課程做為參考資料,來設定現有的 Rails 應用程式以使用 PostgreSQL 適用的 Cloud SQL。
本教學課程支援且需要 Ruby 3.0 以上版本。
事前準備
- 登入 Google Cloud 帳戶。如果您是 Google Cloud新手,歡迎 建立帳戶,親自評估產品在實際工作環境中的成效。新客戶還能獲得價值 $300 美元的免費抵免額,可用於執行、測試及部署工作負載。
-
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.-
安裝 Google Cloud CLI。
-
若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI。
-
執行下列指令,初始化 gcloud CLI:
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.-
安裝 Google Cloud CLI。
-
若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI。
-
執行下列指令,初始化 gcloud CLI:
gcloud init
準備 PostgreSQL 適用的 Cloud SQL 執行個體
為本教學課程設定 PostgreSQL 適用的 Cloud SQL 執行個體。
建立 PostgreSQL 執行個體。在本教學課程中,執行個體的名稱為
rails-cloudsql-instance。在執行個體中建立資料庫。在本教學課程中,實際工作環境的資料庫名稱為
cat_list_production。
設定本機環境以使用 Rails
為本教學課程設定本機環境:
如要進一步瞭解如何安裝 Rails 及其依附元件,請參閱官方的「開始使用 Rails」指南。
完成上述前置步驟後,就能使用 PostgreSQL 適用的 Cloud SQL 建立及部署 Rails 應用程式。以下各節將逐步引導您進行應用程式設定、連結至 PostgreSQL 適用的 Cloud SQL,以及部署應用程式。
建立新應用程式來列出貓咪
執行
rails new指令,建立新的 Rails 應用程式。這個應用程式會在 PostgreSQL 適用的 Cloud SQL 中儲存一份貓咪清單。rails new cat_sample_app前往產生的 Rails 應用程式所屬的目錄。
cd cat_sample_app
在本機執行應用程式
如何在本機電腦上執行新的 Rails 應用程式:
啟動本機網路伺服器:
bundle exec bin/rails server透過瀏覽器前往 http://localhost:3000/
範例應用程式會顯示 Rails 標誌,以及 Rails 和 Ruby 版本。
為貓咪清單產生鷹架
針對構成貓咪清單 (包括貓咪的名字和年齡),名為 Cat 的資源產生 Scaffolding。
產生 Scaffolding。
bundle exec rails generate scaffold Cat name:string age:integer這項指令會針對
Cat資源產生模型、控制器和檢視畫面。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