7

I am starting to work with hotwire/turbo and have set everything up. Turbo streams are working correctly.

Within index.html.erb:

<h1>Tasks</h1>
<%= turbo_stream_from "tasks" %>

<div id="tasks">
  <% @tasks.each do |task| %>
    <%= render task %>
  <% end %>
</div>

Within _task.html.erb

<%= turbo_frame_tag dom_id(task) do %>
  <%= link_to 'Edit', edit_task_path(task) %>
  <%= link_to 'Destroy', task, method: :delete, data: { confirm: 'Are you sure?' } %>
  <hr>
<% end %>

When the destroy button is pressed as expected, rails console says:

Started DELETE "/tasks/41" for ::1 at 2021-12-14 18:40:32 +0000
Processing by TasksController#destroy as TURBO_STREAM

But when pressing the edit button, rails console says:

Started GET "/tasks/41/edit" for ::1 at 2021-12-14 18:41:29 +0000
Processing by TasksController#edit as HTML

This means the page loads a new page, rendering edit.html.erb rather than just updating the content within the turbo_frame_tag.

When inspecting the DOM, both the edit and destroy links are within a turbo-frame:

<turbo-frame id="task_41">
  <a href="/tasks/41/edit">Edit</a>
  <a data-confirm="Are you sure?" rel="nofollow" data-method="delete" href="/tasks/41">Destroy</a>
  <hr>
</turbo-frame> 

So my question is, why does the request get processed as HTML and not TURBO_STREAM ?

edit.html.erb looks like this:

<h1>Editing Task</h1>
<%= turbo_frame_tag dom_id(@task) do %>
    <%= render 'form', task: @task %>
<% end %>
<%= link_to 'Show', @task %> |
<%= link_to 'Back', tasks_path %>

Thanks very much!

daveanderson88
  • 327
  • 1
  • 4
  • 12

2 Answers2

1

Solved:

In application.js, I still had this line:

require("turbolinks").start()

I removed this, which fixed the problem.

daveanderson88
  • 327
  • 1
  • 4
  • 12
0

I have the same issue, and my application doesn't have a require("turbolinks").start() line to remove.

In addition, I feat that when removing that line, every other link_to will be processed as a turbo_stream. And at any point on the development of the application, one will need a link_to that will act as HTML.

Karl
  • 374
  • 1
  • 5
  • 15
  • 2
    You can also try adding `method: :get` to your `link_to` tags. I know they are already get but this trick worked for me. – Dan Tappin Mar 09 '22 at 15:11
  • 1
    I found I needed to add import Rails from "@rails/ujs" and Rails.start() to my application.js file to get the links to work with turbo_stream for get requests. Remember to do yarn add @rails/ujs. – chell Jul 12 '22 at 15:05