User 69e88e27
发布于:2026-01-13
response_type 标签用于设置模板的响应类型,告诉控制器应该以什么格式返回响应内容。当需要返回非 HTML 格式的响应(如 Turbo Stream、JSON、XML)时,通常需要配合 {% layout none %} 使用。{% response_type "类型" %}
html - HTML 格式(默认值)turbo_stream - Turbo Stream 格式,用于 Hotwire Turbo 的流式更新json - JSON 格式,用于 API 接口xml - XML 格式{% response_type "turbo_stream" %}
{% layout none %}
<turbo-stream action="replace" target="content">
<template>
<div id="content">更新后的内容</div>
</template>
</turbo-stream>
append - 在目标元素内部末尾追加内容prepend - 在目标元素内部开头插入内容replace - 替换目标元素的整个内容update - 更新目标元素的 innerHTMLremove - 移除目标元素before - 在目标元素之前插入内容after - 在目标元素之后插入内容{% response_type "turbo_stream" %}
{% layout none %}
{%# 移除旧元素 %}
<turbo-stream action="remove" target="old-element"></turbo-stream>
{%# 更新内容区域 %}
<turbo-stream action="replace" target="content-area">
<template>
<div id="content-area">
{% render 'updated_content', data: data %}
</div>
</template>
</turbo-stream>
{%# 追加新元素 %}
<turbo-stream action="append" target="list-container">
<template>
{% render 'list_item', item: new_item %}
</template>
</turbo-stream>
{% response_type "json" %}
{% layout none %}
{
"status": "success",
"message": "操作成功",
"data": {
"id": 123,
"name": "示例"
}
}
{% response_type "json" %}
{% layout none %}
{
"articles": [
{% for article in articles %}
{
"id": {{ article.id }},
"title": {{ article.title | json }},
"created_at": {{ article.created_at | date: "%Y-%m-%d" | json }}
}{% unless forloop.last %},{% endunless %}
{% endfor %}
]
}
{% response_type "xml" %}
{% layout none %}
<?xml version="1.0" encoding="UTF-8"?>
<response>
<status>success</status>
<message>数据已更新</message>
<data>
<item id="1">内容1</item>
<item id="2">内容2</item>
</data>
</response>
response_type,或者设置为 html,模板会以 HTML 格式返回。{% response_type "html" %}
<!-- 或者不设置 response_type -->
<h1>页面标题</h1>
<p>页面内容</p>
{% response_type "turbo_stream" %}
{% layout none %}
{%# 替换表单,显示验证错误 %}
<turbo-stream action="replace" target="comment-form">
<template>
{% render 'comment_form', comment: comment, errors: errors %}
</template>
</turbo-stream>
{%# 如果成功,追加新评论 %}
{% if comment.persisted? %}
<turbo-stream action="append" target="comments-list">
<template>
{% render 'comment', comment: comment %}
</template>
</turbo-stream>
{% endif %}
{% response_type "json" %}
{% layout none %}
{
"articles": [
{% for article in articles %}
{
"id": {{ article.id }},
"title": {{ article.title | json }},
"excerpt": {{ article.excerpt | json }},
"created_at": {{ article.created_at | date: "%Y-%m-%d" | json }}
}{% unless forloop.last %},{% endunless %}
{% endfor %}
],
"total": {{ articles.size }}
}
response_status 返回错误信息。{% response_status 422 %}
{% response_type "json" %}
{% layout none %}
{
"status": "error",
"message": "验证失败",
"errors": {
"email": ["邮箱格式不正确"],
"password": ["密码长度至少8位"]
}
}
html、turbo_stream、json、xml 四个值,其他值会被忽略,使用默认的 HTML 格式{% layout none %} 禁用布局,避免布局的 HTML 结构(如 <html>、<head>、<body> 等标签)污染响应内容response_type 会覆盖模板中的设置,因此建议在模板中设置响应类型,而不是在布局中response_type 标签放在模板的最前面,确保在渲染过程中正确设置响应类型{%# ❌ 错误示例 %}
{% response_type "json" %}
<!-- 没有设置 layout none -->
{
"status": "success"
}
{%# ✅ 正确示例 %}
{% response_type "json" %}
{% layout none %}
{
"status": "success"
}
{%# ❌ 错误示例 %}
{% response_type "turbo-stream" %} <!-- 应该是 turbo_stream -->
{%# ✅ 正确示例 %}
{% response_type "turbo_stream" %}
{%# ❌ 不推荐的做法 %}
{%# 在模板中 %}
{% response_type "json" %}
{% layout "custom" %}
<!-- 在 layout/custom.liquid 中又设置了 -->
{% response_type "html" %}
{% layout none %} 禁用布局response_type 和 layout 标签放在模板的最前面turbo_stream 而不是 turbo-stream)response_status 返回适当的状态码response_type 标签是控制模板响应格式的关键标签,支持 HTML、Turbo Stream、JSON、XML 四种格式。当使用非 HTML 格式时,必须配合 {% layout none %} 禁用布局,以确保响应内容的格式正确。正确使用此标签可以实现丰富的交互效果,如无刷新页面更新、API 接口、流式响应等。