Turbo 8 morph and GET requests

Hi!
I’ve been trying the 8.0.X update for turbo, since working with multiple turbo_frame files was a bit of a headache I was excited for this new morph thing.

My issue, or question I guess, is: what if i want this kind of layout updates as a consequence of a GET request?
A simple example, a table of records which can be filtered with a form.

<div>
  <%= form_with(url: users_path, method: :get, data: { controller: 'table-filters', turbo_stream: '', turbo_permanent: '' }, html: {id: 'filters'}) do |f| %>
    <%= text_field_tag :full_name, '', data: {action: "input->table-filters#search"} %>
    <%= text_field_tag :email, '', data: {action: "input->table-filters#search"} %>
  <% end %>

  <table>
  <!-- ... @users.each do |u| ... -->
  </table>
</div>

The table-filters controller just does a form.requestSubmit() with a debounce.

To me, using a post for this action doesn’t make sense and, while the data-turbo-stream makes the form accept a stream response, you still see the query string params because it is still a get request.

Should I just accept this? Am I missing something?
I can see multiple use cases where you want to update parts of your layout with strictly GET requests (filters, validation, charts, …) so it feels a bit weird that there’s no immediate way of doing this without falling back on the cumbersome usage of multiple turbo_frame.erb files

Thanks in advance!

In case anyone bumps into here looking for an answer, I figured it out.

First, it’s no inherently bad to see the querystring for get request, sometimes you do want that (SEO, caching ecc).

Having said that, the solution lies in a small section of a short paragraph in the docs (here).
Basically, turbo expects certain status codes as response in order to morph after a form submission, and the usual code returned for the GET action is not one of those.

All that was needed was to update the controller action to return a status: 303 if the request format is a :turbo_stream

Hope this saves time to someone someday :vulcan_salute:

1 Like