Bolo
Bolo

Reputation: 2778

Turbo_stream format not being sent anymore

we are currently working on changing our web app search engine from Stimulus to Turbo.

However, we keep getting an HTML request instead of a Turbo one from our script with the error : “

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: []: “

We are on the 7.0.0-beta.5 Turbo version.

View

<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

Any advises ?

Upvotes: 6

Views: 6611

Answers (3)

David Morales
David Morales

Reputation: 18064

When defining a route manually with get, for example, you should specify the default format like this:

get :search, to: 'search#index', defaults: { format: :turbo_stream }

Upvotes: 4

RedZagogulin
RedZagogulin

Reputation: 461

Just encounter the very same problem: form with method: :get does not send a request with TURBO_STREAM format (it is HTML).

Aidan's answer is almost correct - format: :turbo_stream should be specified explicitly. It should not be an argument's hash element but a URL's parameter, like this:

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

Upvotes: 9

aidan
aidan

Reputation: 1785

I may have just had a similar problem. I think you need to specify format: :turbo_stream as one of your options to form_with. In my case, I wanted to hit the HTML block but the form kept hitting turbo_stream instead.

To be extra clear, this

<%= form_with url: "/search", method: :get , data: { controller: "search" }  do |form| %>

becomes this

<%= form_with url: "/search", method: :get , data: { controller: "search" }, format: :turbo_stream  do |form| %>

Upvotes: 0

Related Questions