Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to render layouts when using inline turbo stream responses #653

Open
spohlenz opened this issue Jul 31, 2024 · 1 comment
Open

Unable to render layouts when using inline turbo stream responses #653

spohlenz opened this issue Jul 31, 2024 · 1 comment

Comments

@spohlenz
Copy link

I've found an inconsistency with the rendering of layouts and inline turbo stream responses, with the combination of the two currently being broken.

When rendering a turbo stream template action.turbo_stream.erb, it is rendered within a layout (e.g. application.turbo_stream.erb) if available. However when using render turbo_stream: turbo_stream.etc..., no layout is used. Furthermore, it is not possible to force a layout to be used by passing layout: true or layout: "application".

class TestController < ApplicationController
  def turbo_template
    # Renders template with application layout
  end

  def turbo_template_layout_true
    # Renders template with application layout
    render layout: true
  end

  def turbo_inline
    # Renders with no layout
    render turbo_stream: turbo_stream.append("container") { "Hello from inline Turbo Stream" }
  end

  def turbo_inline_layout_true
    # Renders with no layout (!)
    render turbo_stream: turbo_stream.append("container") { "Hello from inline Turbo Stream (layout: true)" }, layout: true
  end

  def turbo_inline_explicit_layout
    # Renders with no layout (!)
    render turbo_stream: turbo_stream.append("container") { "Hello from inline Turbo Stream (explicit layout)" }, layout: "application"
  end
end

I have created an example repo at https://github.com/spohlenz/turbo-stream-layouts (with the relevant changes in spohlenz/turbo-stream-layouts#8afd217d005f2f852bf27fb871ac4bbf7291d72a). This example uses Rails 7.2.0.beta3 and turbo-rails 2.0.6 but I have seen the same behavior on earlier Rails versions.

@4lllex
Copy link

4lllex commented Aug 24, 2024

You could override :turbo_stream renderer if you really need this:

# config/initializers/turbo_layout.rb

# on load hook is to make sure to load this after Turbo adds its renderer
ActiveSupport.on_load(:action_controller) do
  ActionController::Renderers.add :turbo_stream do |source, options|
    self.content_type = Mime[:turbo_stream] if media_type.nil?
    render_to_string(inline: source, layout: options[:layout])
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants