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

Not updating turbo frames on POST when rendering from controllers #1440

Closed
MatheusPoliCamilo opened this issue Jul 25, 2022 · 4 comments · Fixed by #1442
Closed

Not updating turbo frames on POST when rendering from controllers #1440

MatheusPoliCamilo opened this issue Jul 25, 2022 · 4 comments · Fixed by #1442

Comments

@MatheusPoliCamilo
Copy link
Contributor

MatheusPoliCamilo commented Jul 25, 2022

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

@MatheusPoliCamilo
Copy link
Contributor Author

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.

@joelhawksley
Copy link
Member

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

@MatheusPoliCamilo
Copy link
Contributor Author

MatheusPoliCamilo commented Jul 26, 2022

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

@lazylester
Copy link

@MatheusPoliCamilo are you using haml for your views? I was facing the same issue you reported, and came across this: hotwired/turbo-rails#287 (comment). It seems that rendering haml files doesn't trigger the appropriate content_type header. Specifying the content_header in the controller is a workaround. It seems to me like a change to haml code is indicated. Or you can change your filenames to filename.html.haml iso filename.haml

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

Successfully merging a pull request may close this issue.

3 participants