在 Ruby on Rails 中使用 Supabase
Learn how to create a Rails project and connect it to your Supabase Postgres database.
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.
想通过编程创建一个项目吗?
使用 管理 API 或向 MCP 服务器 请求创建一个新的 Supabase 项目。
🌐 Use the Management API or ask the MCP server to create a new Supabase project.
安全地保存你的数据库密码。你连接数据库时需要它。
🌐 Save your database password securely. You need it for the connection string.
本指南使用 Rails 自己的 Active Record 模型和迁移,而不是其他快速入门使用的共享 instruments 示例表。
🌐 This guide uses Rails' own Active Record models and migrations, not the shared instruments sample table used by other quickstarts.
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.
rails new blog -d=postgresqlcd blog3. 设置 AI 工具(可选) #
🌐 3. Set up AI tooling (optional)
Supabase 提供了两种方式让 AI 工具了解你的项目:Agent Skills,它为你的 AI 编程助手提供操作知识;以及 MCP 服务器,它可以让 AI 助手直接连接到你的 Supabase 项目。
🌐 Supabase provides two ways to give AI tools context about your project: Agent Skills, which give your AI coding agent procedural knowledge, and the MCP server, which connects AI assistants to your Supabase project directly.
代理技能 #
🌐 Agent Skills
代理技能 是一套精心整理的指令,提供你的 AI 代理有关使用 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.
安装代理技能 #
🌐 Installing Agent Skills
要安装,在你项目的根目录运行以下命令:
🌐 To install, run the following command in the root of your project:
npx skills add supabase/agent-skillsSupabase MCP 服务器 #
🌐 Supabase MCP server
Supabase MCP 服务器将 AI 助手连接到 Supabase,这样它们就可以检查你的架构并代表你操作项目。在 MCP 文档 中了解如何将它添加到你的客户端。
🌐 The Supabase MCP server connects AI assistants to Supabase, so they can inspect your schema and act on your projects on your behalf. Find out how to add it to your client in the MCP docs.
4. 设置 Postgres 连接详情 #
🌐 4. Set up the Postgres connection details
-
导航到你的项目仪表板,然后点击连接。
-
找到会话池连接字符串并复制它。将密码占位符替换为你保存的数据库密码,并对其中的任何保留字符进行百分号编码,比如
&、#、?或空格。如果你没有数据库密码,可以在你的数据库设置中重置它。 -
如果你的框架单独设置它,可以在连接字符串上或作为显式配置选项设置
sslmode=require。大多数驱动默认使用prefer,如果加密尝试失败,就会回退到以明文发送数据。你也可以在数据库端强制使用 SSL。
下面的连接字符串只是显示格式。请从你复制的字符串中获取主机、端口和用户名,而不是直接输入括号里的占位符。
🌐 The connection strings below show the format only. Take the host, port, and username from the string you copied rather than typing the bracketed placeholders literally.
将连接字符串设置为环境变量。Rails 会从环境中读取 DATABASE_URL 并用它进行连接,所以你不需要编辑 config/database.yml。export 只对当前的 shell 会话有效,所以在执行下面的 Rails 命令时,要在同一个 shell 中运行它。
🌐 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.
export DATABASE_URL=postgres://postgres.[PROJECT-REF]:[YOUR-PASSWORD]@[POOLER-HOST]:5432/postgres?sslmode=require5. 创建并运行数据库迁移 #
🌐 5. 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.
bin/rails generate model Article title:string body:textbin/rails db:migrate6. 使用模型与数据库交互 #
🌐 6. 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.
bin/rails consolearticle = Article.new(title: "Hello Rails", body: "I am on Rails!")article.save # Saves the entry to the databaseArticle.all7. 启动应用 #
🌐 7. 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.
bin/rails server生产要求 #
🌐 Production requirements
本指南中的快速入门步骤是为了让你尽快运行起一个可用的应用,而不是为了生产环境优化。
🌐 The quickstart procedure in this guide optimizes for getting you to a working app, not for production.
在你部署之前:
🌐 Before you deploy:
- 如果你的应用通过数据 API 读取或写入数据,请查看你的 行级安全 策略。你在这里添加的任何策略都是针对这个快速入门的示例数据,而不是针对真实用户数据。
- 在你部署的平台上把 Supabase 凭据设置为环境变量,而不是把它们提交到源码管理里。
- 一旦你准备上线,就为你的 Supabase 项目配置一个自定义域名。
下一步 #
🌐 Next steps