Home > Software design >  turbo_frame_tag request with other formats than HTML
turbo_frame_tag request with other formats than HTML

Time:01-25

Given the following

<%= turbo_frame_tag dom_id(@item, "loader_helper"), src: home_index_path(@item) %>

This code will trigger with page load other request to specified controller

Started GET "/home/1" for ::1 at 2023-01-23 16:10:39  0300
Processing by HomeController#index as HTML

The problem is that I want to render not HTML, but rather turbo_stream format with some page modifications and don't rely on some JS solution.

So I would expect the following pseudocode to work

<%= turbo_frame_tag dom_id(@item, "loader_helper"), src: home_index_path(@item), format: :turbo_stream %>

to load action like so

Started GET "/home/1" for ::1 at 2023-01-23 16:10:39  0300
Processing by HomeController#index as TURBO_STREAM

and then in controller I can handle it with specific formats

..
def index
  respond_to do |format|
    format.html
    format.turbo_stream
  end
end

Are there any workarounds? Or this is intended by design, so we won't be able to trigger turbo_stream on page load (you can do it with js easily), and manipulate with some HTML.

CodePudding user response:

Based on the doc https://turbo.hotwired.dev/handbook/streams it injects text/vnd.turbo-stream.html for POST, PUT, PATCH, or DELETE HTTP method. If you want to use it for the GET method, you must add the data-turbo-stream attribute. So for your case, you can try the following code:

<%= turbo_frame_tag dom_id(@item, "loader_helper"), src: home_index_path(@item, format: :turbo_stream) %>

Reference: https://github.com/hotwired/turbo-site/pull/40#discussion_r570471371

  • Related