googlesnet
googlesnet

Reputation: 95

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?

Upvotes: 3

Views: 2330

Answers (2)

Brendon
Brendon

Reputation: 947

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 } %>

Upvotes: 6

Tim Parker
Tim Parker

Reputation: 41

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.

Upvotes: 2

Related Questions