Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Render inside turbo_stream template tag

Why rendering partial inside <template> tags of stream.erb file raises ActionView::MissingTemplate error?

For example in some_action_in_controller.turbo_stream.erb file i have;

<turbo-stream action="some_action" target="some_target">
  <template>
    <div class="some-class">
      <% @some_records.each do |r|
       <%= render partial: 'some_partial', locals: { data_to_partial } %>
      <% end% >
    </div>
  </template>
</turbo-stream>

In this case i use <turbo-stream> tag instead of <%= turbo_stream.. %> since in my stream file, i render more than just a partial to maintain the UI in page where i am streaming the data.

Why am i getting the error? and what is actually happening?

like image 444
googlesnet Avatar asked Feb 14 '26 04:02

googlesnet


2 Answers

Add formats: [:html] to the render statement. Note that this is formats plural, unlike the previous answers.

<%= render partial: 'some_partial', formats: [:html], locals: { data_to_partial } %>
like image 77
Brendon Avatar answered Feb 21 '26 14:02

Brendon


You can also change your partial's format, rename from:

some_partial.html.erb to some_partial.turbo_stream.erb.

Specifying the format: [:html] on render didn't work for me on rails 7.0.2.4, turbo-rails 1.0.1.

like image 24
Tim Parker Avatar answered Feb 21 '26 15:02

Tim Parker