我正在尝试从 Turbolinks 升级到 Turbo,但我发现客户端没有为表单提交呈现重定向。
版本:
- 导轨 6.1.4
- hotwire-rails 0.1.2
- @hotwired/turbo-rails 7.0.0-beta.8
我暂时忽略了 Turbo 和 Devise 之间的不兼容性——只是试图让常规表单工作而不必禁用它们上的 Turbo。
这是一个示例操作:
def update
authorize @label
@label.update(label_params)
if @label.save
redirect_to document_labels_path(document_id: @document.id)
else
render :new, status: :unprocessable_entity
end
end
这是一个呈现的形式:
<form class="simple_form new_label" id="label_form" novalidate="novalidate" action="/documents/72/labels" accept-charset="UTF-8" method="post">
...
</form>
提交有效表单时,服务器会说Processing by LabelsController#create as TURBO_STREAM
并正确提供 302。然后它将为重定向位置提供 200。然而,浏览器只查看提交的表单。将重定向状态更改为 303 不会改变任何内容。
我为每个 Turbo 事件添加了一个 console.log:
document.addEventListener("turbo:load", function () {
console.log('TURBO:LOAD')
})
document.addEventListener("turbo:click", function () {
console.log('TURBO:CLICK')
})
document.addEventListener("turbo:before-visit", function () {
console.log('TURBO:BEFORE-VISIT')
})
document.addEventListener("turbo:visit", function () {
console.log('TURBO:VISIT')
})
document.addEventListener("turbo:submit-start", function () {
console.log('TURBO:SUBMIT-START')
})
document.addEventListener("turbo:before-fetch-request", function () {
console.log('TURBO:BEFORE-FETCH-REQUEST')
})
document.addEventListener("turbo:before-fetch-response", function () {
console.log('TURBO:BEFORE-FETCH-RESPONSE')
})
document.addEventListener("turbo:submit-end", function (event) {
console.log('TURBO:SUBMIT-END')
// event.detail
})
document.addEventListener("turbo:before-cache", function () {
console.log('TURBO:BEFORE-CACHE')
})
document.addEventListener("turbo:before-stream-render", function () {
console.log('TURBO:BEFORE-STREAM-RENDER')
})
document.addEventListener("turbo:render", function () {
console.log('TURBO:RENDER')
})
这是成功提交表单的输出:
TURBO:BEFORE-FETCH-REQUEST
TURBO:SUBMIT-START
TURBO:BEFORE-FETCH-RESPONSE
TURBO:SUBMIT-END
没有渲染事件。调查它似乎完全意识到客户端应该重定向,它只是没有event.detail.fetchResponse.response
。turbo:submit-end
Response {type: "basic", url: "http://lvh.me:3000/documents/72/labels", redirected: true, status: 200, ok: true, …}
body: (...)
bodyUsed: true
headers: Headers {}
ok: true
redirected: true
status: 200
statusText: "OK"
type: "basic"
url: "http://lvh.me:3000/documents/72/labels"
__proto__: Response