GithubHelp home page GithubHelp logo

Comments (5)

chloerei avatar chloerei commented on June 5, 2024 9

But I found a document say that format is automatically switched:

code:

# <%= turbo_stream.append "entries" do %>
# <% # format is automatically switched, such that _entry.html.erb partial is rendered, not _entry.turbo_stream.erb %>
# <%= render partial: "entries/entry", locals: { entry: entry } %>
# <%= end %>

commit: 969d899

Did I misunderstand?

from turbo-rails.

scomma avatar scomma commented on June 5, 2024 9

I'd like to request a reopening of this issue.

Currently this results in an unintuitive behavior where if you have a controller action do implicit rendering to a Turbo Stream template, and that template renders (via a standard render method) an HTML partial, the format switching happens automatically.

Once you so much as add respond_to to the controller action, that format switching turns off, and you have to modify the render call inside the first template to explicitly request the HTML partial.

Illustration below. Hope this makes sense.

Works

x_controller.rb

  def new
  end

new.turbo_stream.erb

render 'form', x: ...

_form.html.erb

<form>...

Breaks

x_controller.rb

  def new
    respond_to do |format|
      format.turbo_stream
      format.html { redirect_to ... }
    end
  end

new.turbo_stream.erb

render 'form'

_form.html.erb

<form>...

Workaround

x_controller.rb

  def new
    respond_to do |format|
      format.turbo_stream
      format.html { redirect_to ... }
    end
  end

new.turbo_stream.erb

render partial: 'form', formats: :html, locals: { x: ... }

_form.html.erb

<form>...

from turbo-rails.

kylekeesling avatar kylekeesling commented on June 5, 2024 2

@chloerei you may have already figured this out but the docs you are referring to reference the call that DHH mentioned, meaning if you use the turbo_stream.append helper you get the format switching "automatically", but if you are using the standard render call you'll need to explicity define formats: [:html]

from turbo-rails.

dhh avatar dhh commented on June 5, 2024

That is the correct behavior in the sense that the turbo stream template is assuming all partials will also be turbo stream templates, unless you change that. We have a helper when you do the inline call, though:

<%= turbo_stream.append 'messages', @message %>

It also work with the partial options:

<%= turbo_stream.append 'messages', partial: "messages/message" %>

But if you call your own render partial, you need to add the formats options.

Feel free to look at a doc change to make this clearer.

from turbo-rails.

joshmfrankel avatar joshmfrankel commented on June 5, 2024

As @kylekeesling mentions above, a standard render call requires the format to be explicitly defined. For example, a failure case from form submission may look like:

render :show, status: :unprocessable_entity, formats: :html

Reason being the resulting error: ActionView::MissingTemplate indicates that it is trying to render :formats=>[:turbo_stream] which if you only have the HTML template it isn't going to work. (what DHH said above) Hence the above explicit definition is necessary

from turbo-rails.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.