Skip to content
Getting Started

在 Ruby on Rails 中使用 Supabase

Learn how to create a Rails project and connect it to your Supabase Postgres database.

AI Prompt
Help me add Supabase to my Ruby on Rails project. Create a Supabase project at database.new. Then: 1. Run `rails new blog -d=postgresql` to scaffold a new Rails project. 2. Set `DATABASE_URL` to the Supabase Session Pooler connection string in `.env`. 3. Generate an Article model with `bin/rails generate model Article title:string body:text` and run `bin/rails db:migrate`. 4. Use `bin/rails console` to create and query articles. 5. Run `bin/rails server` and open http://127.0.0.1:3000. REFERENCE https://supabase.com/docs/guides/getting-started/quickstarts/ruby-on-rails.md

1. 创建一个 Supabase 项目 #

🌐 1. Create a Supabase project

首先,你需要一个 Supabase 项目。

🌐 To start, you need a Supabase project.

从你所属的任何组织的仪表板创建一个新的 Supabase 项目。

🌐 Create a new Supabase project from the Dashboard of any organization you belong to.

安全地保存你的数据库密码。你连接数据库时需要它。

🌐 Save your database password securely. You need it for the connection string.

2. 创建一个 Rails 项目 #

🌐 2. Create a Rails project

在你的 Ruby 和 Rails 版本更新到最新后,在终端运行 rails new 来搭建一个新项目。

🌐 With your Ruby and Rails versions up to date, run rails new on your terminal to scaffold a new project.

使用 -d=postgresql 标志来为 Postgres 设置它。

🌐 Use the -d=postgresql flag to set it up for Postgres.

查看 Rails 文档 获取更多详细信息。

🌐 Check the Rails docs for more details.

1
rails new blog -d=postgresql
2
cd blog

3. 安装 Supabase 的代理技能(可选) #

🌐 3. Install Supabase's Agent Skills (optional)

Supabase 的 Agent Skills 是一套精心整理的指令,给你的 AI 代理提供关于如何使用 Supabase 的操作知识。

🌐 Supabase's Agent Skills is a curated set of instructions that give your AI agent procedural knowledge about working with Supabase.

安装它们,这样你的 AI 编码代理就能使用当前的 Supabase 模式(比如认证、服务端渲染和数据库迁移)生成更准确、更可靠的代码,而不只是依赖培训数据。

🌐 Install them so your AI coding agent can produce more accurate, reliable code using current Supabase patterns, such as authentication, server-side rendering, and database migrations, rather than relying solely on training data.

要安装,在你项目的根目录运行以下命令:

🌐 To install, run the following command in the root of your project:

1
npx skills add supabase/agent-skills

4. 安装 MCP 服务器(可选) #

🌐 4. Install MCP server (optional)

Supabase MCP 服务器将 AI 助手连接到 Supabase,让你能够代表自己与项目互动。想了解如何将它添加到你的客户端,请查看 MCP 文档

🌐 The Supabase MCP server connects AI assistants to Supabase, allowing you to interact with your projects on your behalf. Find out more on how to add it to your client in the MCP docs.

5. 设置 Postgres 连接详情 #

🌐 5. Set up the Postgres connection details

导航到你的项目仪表板,然后点击连接

🌐 Navigate to your project dashboard and click on Connect.

查找 Session Pooler 的连接字符串并复制。你需要将 Password 替换为你保存的数据库密码,并对其中的任何保留字符进行百分比编码,比如 &#? 或空格。如果你没有数据库密码,可以在数据库设置中重置。

🌐 Look for the Session Pooler connection string and copy the string. You will need to replace the Password with your saved database password, and percent-encode any reserved characters it contains, such as &, #, ?, or a space. You can reset your database password in your Database Settings if you do not have it.

将连接字符串设置为环境变量。Rails 会从环境中读取 DATABASE_URL 并用它连接,所以你不需要修改 config/database.ymlexport 只对当前的 shell 会话有效,所以请在接下来执行 Rails 命令的同一个 shell 中运行它。sslmode=require 防止驱动程序回退到以明文方式发送你的数据。

🌐 Set the connection string as an environment variable. Rails reads DATABASE_URL from the environment and connects with it, so you don't need to edit config/database.yml. The export applies to the current shell session, so run it in the same shell as the Rails commands in the following steps. sslmode=require stops the driver from falling back to sending your data in plaintext.

1
export DATABASE_URL=postgres://postgres.[PROJECT-REF]:[YOUR-PASSWORD]@aws-[REGION].pooler.supabase.com:5432/postgres?sslmode=require

6. 创建并运行数据库迁移 #

🌐 6. Create and run a database migration

Rails 包含 Active Record 作为 ORM,还提供数据库迁移工具,会为你生成 SQL 迁移文件。

🌐 Rails includes Active Record as the ORM as well as database migration tooling which generates the SQL migration files for you.

创建一个示例 Article 模型并生成迁移文件。

🌐 Create an example Article model and generate the migration files.

1
bin/rails generate model Article title:string body:text
2
bin/rails db:migrate

7. 使用模型与数据库交互 #

🌐 7. Use the model to interact with the database

你可以使用随附的 Rails 控制台与数据库互动。例如,你可以创建新的条目,或者列出某个模型表的所有条目。

🌐 You can use the included Rails console to interact with the database. For example, you can create new entries or list all entries in a Model's table.

1
bin/rails console
irb
1
article = Article.new(title: "Hello Rails", body: "I am on Rails!")
2
article.save # Saves the entry to the database
3
4
Article.all

8. 启动应用 #

🌐 8. Start the app

运行开发服务器。在浏览器中打开 http://127.0.0.1:3000 来查看你的应用运行情况。

🌐 Run the development server. Go to http://127.0.0.1:3000 in a browser to see your application running.

1
bin/rails server

下一步 #

🌐 Next steps