<!-- Generated from zh/llms-txt-rails/index.html. The canonical document is the HTML page. -->

- [ 首页 ](/zh/) 
/
- [ 如何创建 llms.txt ](/zh/how-to-create/) 
/
- Ruby on Rails            
# Ruby on Rails 中的 `llms.txt`

Rails 会通过网站根目录提供 public/ 文件夹。请将 llms.txt 添加到该文件夹；如果内容是动态的，也可以通过控制器生成。

最近更新: 2026年7月26日

本页内容

- [ 两种方法 ](#overview)
- [ 方法 1：public/ 中的静态文件 ](#public)
- [ 方法 2：控制器操作 ](#controller)
- [ 在生产环境提供静态文件 ](#production)
- [ 验证设置 ](#verify)            
## 两种方法

Rails 可以提供 `llms.txt` 两种方式：放在 `public/` （最简单）， 或使用从数据构建文件的控制器操作。除非内容必须反映实时记录，否则请使用静态文件。

## 方法 1：public/ 中的静态文件

位于 `public/` 会在域名根目录提供。位于 `public/llms.txt`
会解析为 `/llms.txt`.

- 创建文件： `touch public/llms.txt` 
- 编写你的内容（参见 [操作指南](/zh/how-to-create/)):   
```
# Your App Name

> One-sentence description of your app for LLM context.

## Core pages

- [Home](https://yourdomain.com/): what this app does.
- [Pricing](https://yourdomain.com/pricing/): plans and limits.
- [Docs](https://yourdomain.com/docs/): developer documentation.

```

## 方法 2：控制器操作

要动态生成该文件，请添加一个返回纯文本的路由和控制器：

```
# config/routes.rb
get "/llms.txt", to: "llms#show"

# app/controllers/llms_controller.rb
class LlmsController   Description of your app.\n\n## Core pages\n\n"
body += pages.map { |p| "- [#{p.title}](#{page_url(p)}): #{p.summary}" }.join("\n")
render plain: body, content_type: "text/plain"
end
end

```

## 在生产环境提供静态文件

警告

public_file_server

默认情况下，Rails 不会在生产环境中提供  public/  ；在生产环境中由前置 Web 服务器或 CDN
提供服务。 如果必须由 Rails 本身提供该文件（例如在某些 PaaS 上），请设置
config.public_file_server.enabled = true  或
RAILS_SERVE_STATIC_FILES  环境变量。无论此设置如何，控制器方法（方法 2）都会由 Rails 提供。

## 验证设置

```
curl -sI https://yourdomain.com/llms.txt | grep -i content-type
# expect: content-type: text/plain
```

改用 Laravel 吗？请参阅 [Laravel 指南](/zh/llms-txt-laravel/)。如需更完整地
导出页面内容，请添加 [`llms-full.txt`](/zh/llms-full-txt/).

## 来源

- [ Rails 指南，配置（public_file_server） ](https://guides.rubyonrails.org/configuring.html)
- [ llmstxt.org，规范参考 ](https://llmstxt.org/)
