ViewComponent/view_component

Not updating turbo frames on POST when rendering from controllers

MatheusPoliCamilo opened this issue · 4 comments

Description

When rendering a ViewComponent from the controllers, it isn't updating the turbo frames tags specifically on POST. Using Rails partials it works fine. Using a Rails partial with a ViewComponent inside works too.

Steps to reproduce

I made an example in this repository: https://github.com/MatheusPoliCamilo/view_component_1440

Basically a request to this ExampleController#create will updates a turbo frame (if the partial renders a turbo frame tag with the expected ID):

class ExampleController < ApplicationController
  def create
    render partial: "example"
  end
end

But the same isn't occurring with the ViewComponent, this code doesn't update a turbo frame (considering that it will render a turbo frame tag with the expected ID):

class ExampleController < ApplicationController
  def create
    render ExampleViewComponent.new
  end
end

Expected behavior

Expected to the rendered ViewComponent by Controller updates the turbo frame when finishes the POST request, like when using Rails partials.

Actual behavior

The rendered ViewComponent by Controller isn't updating the turbo frame tag.

Workaround

Specify the content_type: "text/html" on the Controller render, since the response comes with the type "text/vnd.turbo-stream.html".

class ExampleController < ApplicationController
  def create
    render ExampleViewComponent.new, content_type: "text/html"
  end
end

System configuration

Rails version: 7.0.3.1

Ruby version: 3.1.2

Gem version: 2.61.1

Turbo rails version: 1.1.1

I just found that the problem is because of the response content type that comes with text/vnd.turbo-stream.html instead of text/html. Added the content_type: "text/html" to the controller render and it worked fine.

@MatheusPoliCamilo thanks for the follow-up. Should we close this issue? Would an update to the docs be helpful?

@joelhawksley I've updated the docs, could you take a look in #1442?