ruby-on-railshotwire-rails

How to get a trubo_stream response when clicking on a link within a turbo_frame?


I have a controller with an action search and two views for this action:

In search.html.slim I have a turbo-frame with a link to this same action like:

h1 Title

turbo-frame#results
  = link_to "search", search_path
  = "default result"

In search.turbo_stream.slim

= turbo_stream.replace :results
  = link_to "search again", search_path
  = "new results!"

When I click on the link "search", the controller action respond with the view search.html.slim and not search.turbo_stream.slim...

How to get a turbo_stream response (i.e. a response using search.turbo_stream.slim) by clicking on the link "search" ?


Solution

  • Solution: add an attribute data-turbo-stream to the link, https://turbo.hotwired.dev/handbook/streams#streaming-from-http-responses

    Links become

    = link_to "search", search_path, 'data-turbo-stream': ''