首页 > 解决方案 > Turbo_stream 格式不再发送

问题描述

我们目前正在努力将我们的网络应用搜索引擎从 Stimulus 更改为 Turbo。

然而,我们不断从我们的脚本中收到一个 HTML 请求而不是 Turbo 请求,并出现错误:“</p>

ActionController::UnknownFormat “.
Trying to force the request into the Turbo format give the following error :
“ ActionController::UnknownFormat - SearchController#search is missing a template for this request format and variant.
request.formats: ["text/html"]
request.variant: []: “

我们使用的是 7.0.0-beta.5 Turbo 版本。

看法

<h1>All Apps</h1>
<%= turbo_frame_tag "record" do %>
  <%= form_with url: "/search", method: :get , data: { controller: "search" }  do |form| %>
    <%= form.text_field :search , data: { action: "input->search#findResults" } %>
  <% end %>
<% end %>
<%= turbo_frame_tag "apps" do %>
  <ul>
    <% @apps.each do |app| %>
      <%= content_tag :li, app.name %>
    <% end %>
  </ul>
<% end %>

<turbo-stream action="update" target="apps">
  <template>
    <ul>
      <% @apps.each do |app| %>
        <%= content_tag :li, app.name %>
      <% end %>
    </ul>
  </template>
</turbo-stream>

class SearchController < ApplicationController
  def search
    @apps = ItunesConnect::App.where("name like ?", "#{params[:search]}%")
    respond_to do |format|
       format.turbo_stream
       #format.html  { redirect_to root_url(search: params[:search]) } #for testing
    end
  end
end

有什么建议吗?

标签: ruby-on-railshotwire-railsturbo-rails

解决方案


只是遇到了同样的问题:form withmethod: :get不发送带有TURBO_STREAM格式的请求(它是HTML)。

艾丹的回答几乎是正确的——format: :turbo_stream应该明确说明。它不应该是参数的散列元素,而是 URL 的参数,如下所示:

<%= form_with URL: search_path(format: :turbo_stream), method: :get , data: { controller: "search" } do |form| %>

推荐阅读