User 69e88e27
发布于:2026-01-13
response_status 标签用于设置模板响应的 HTTP 状态码。默认情况下,模板响应状态码为 200(成功),当需要返回其他状态码(如 404、403、422 等)时,可以使用此标签。{% response_status 状态码 %}
{% response_status 201 %}
{% response_type "json" %}
{% layout none %}
{
"status": "created",
"id": 123,
"message": "资源创建成功"
}
{% response_status 404 %}
{% layout none %}
<div class="error-page">
<h1>404 - 页面未找到</h1>
<p>抱歉,您访问的页面不存在。</p>
</div>
{% response_status 403 %}
{% layout none %}
<div class="error-page">
<h1>403 - 禁止访问</h1>
<p>您没有权限访问此页面。</p>
</div>
{% response_status 422 %}
{% response_type "json" %}
{% layout none %}
{
"status": "error",
"message": "验证失败",
"errors": {
"email": ["邮箱格式不正确"],
"password": ["密码长度至少8位"]
}
}
{% response_status 422 %}
{% response_type "turbo_stream" %}
{% layout none %}
<turbo-stream action="replace" target="form-errors">
<template>
<div id="form-errors">
<ul>
{% for error in errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
</div>
</template>
</turbo-stream>
{% response_status 500 %}
{% layout none %}
<div class="error-page">
<h1>500 - 服务器错误</h1>
<p>服务器遇到了一个错误,请稍后再试。</p>
</div>
{%# statics/404.liquid %}
{% response_status 404 %}
{% layout none %}
<!DOCTYPE html>
<html>
<head>
<title>404 - 页面未找到</title>
<style>
.error-container {
text-align: center;
padding: 50px;
}
</style>
</head>
<body>
<div class="error-container">
<h1>404</h1>
<p>抱歉,您访问的页面不存在。</p>
<a href="/">返回首页</a>
</div>
</body>
</html>
{%# templates/create_comment_turbo_stream.liquid %}
{% response_status 422 %}
{% response_type "turbo_stream" %}
{% layout none %}
{%# 显示验证错误 %}
<turbo-stream action="replace" target="comment-form">
<template>
<form id="comment-form">
{% if errors %}
<div class="errors">
<ul>
{% for error in errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
</div>
{% endif %}
{% render 'comment_form', comment: comment %}
</form>
</template>
</turbo-stream>
{%# statics/api/error.liquid %}
{% response_status 400 %}
{% response_type "json" %}
{% layout none %}
{
"status": "error",
"code": 400,
"message": "请求参数错误",
"details": {
"field": "email",
"reason": "邮箱格式不正确"
}
}
{% response_status 201 %}
{% response_type "json" %}
{% layout none %}
{
"status": "created",
"id": {{ article.id }},
"title": {{ article.title | json }},
"created_at": {{ article.created_at | date: "%Y-%m-%d %H:%M:%S" | json }}
}
{% response_status 403 %}
{% layout none %}
<div class="error-page">
<h1>403 - 禁止访问</h1>
<p>您没有权限访问此资源。</p>
{% if current_user %}
<a href="/">返回首页</a>
{% else %}
<a href="/sign_in">登录</a>
{% endif %}
</div>
response_type 和 layout 标签配合使用,实现完整的响应控制response_status 会覆盖模板中的设置response_status 标签放在模板的最前面,与其他控制标签一起{%# ❌ 错误示例 %}
{% response_status 100 %} <!-- 超出范围 -->
{% response_status 600 %} <!-- 超出范围 -->
{%# ✅ 正确示例 %}
{% response_status 404 %}
{%# ❌ 错误示例 %}
{% response_status "404" %} <!-- 字符串 -->
{%# ✅ 正确示例 %}
{% response_status 404 %} <!-- 整数 -->
{%# ❌ 错误示例 %}
{% response_status 404 %}
<!-- 没有设置 layout none,如果返回非 HTML 格式会有问题 -->
{%# ✅ 正确示例 %}
{% response_status 404 %}
{% layout none %}
<div>404 错误页面</div>
response_type(如 JSON、HTML){% layout none %} 禁用布局response_status 标签放在模板的最前面,与其他控制标签一起response_status 标签用于设置模板响应的 HTTP 状态码,支持 200-599 范围内的状态码。正确使用此标签可以实现适当的 HTTP 响应,如错误处理、资源创建确认、权限控制等。通常需要与 response_type 和 layout 标签配合使用,以实现完整的响应控制。