在 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.
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.
1rails new blog -d=postgresql2cd blog3. 安装 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:
1npx skills add supabase/agent-skills4. 安装 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.
如果你处在IPv6 环境下,或者拥有 IPv4 附加功能,你可以在会话模式下使用直接连接字符串,而不是 Supavisor。
🌐 If you're in an IPv6 environment or have the IPv4 Add-On, you can use the direct connection string instead of Supavisor in Session mode.
将连接字符串设置为环境变量。Rails 会从环境中读取 DATABASE_URL 并用它连接,所以你不需要修改 config/database.yml。export 只对当前的 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.
1export DATABASE_URL=postgres://postgres.[PROJECT-REF]:[YOUR-PASSWORD]@aws-[REGION].pooler.supabase.com:5432/postgres?sslmode=require6. 创建并运行数据库迁移 #
🌐 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.
1bin/rails generate model Article title:string body:text2bin/rails db:migrate7. 使用模型与数据库交互 #
🌐 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.
1bin/rails console1article = Article.new(title: "Hello Rails", body: "I am on Rails!")2article.save # Saves the entry to the database34Article.all8. 启动应用 #
🌐 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.
1bin/rails server下一步 #
🌐 Next steps