data-turbo-stream="false" do not working for form with method POST

325 views Asked by At

I'm trying to perform a POST request through a form, but i don't want to a turbo_stream request be performed; I want to perform an HTML request.

I tried put data-turbo-stream="false" in the form tag, but it keep executing a turbo-stream request.

<%= form_with(model: book, data: {turbo_stream: false}) do |form| %>

This is the generated HTML:

<form data-turbo-stream="false" action="/books" accept-charset="UTF-8" method="post"><input type="hidden" name="authenticity_token" value="t1PouR2NA4bSleg8OBSRQhvAP1DTSmCkchiTf4aqUIWuPJjG9pJk3J58Z25o9-S2ZXhqFwaUSTfWFn0LvQZF3Q" autocomplete="off">

Ruby version: ruby 3.2.2 (2023-03-30 revision e51014f9c0) [x64-mingw-ucrt]

Rails version: Rails 7.0.8

Thanks!!

This is my create method. Always it's responding in TRUBO_STREAM format:

# POST /books or /books.json
  def create
    @book = Book.new(book_params)

    respond_to do |format|

      if @book.save
        format.html { redirect_to book_url(@book), notice: "Book was successfully created." }
        format.turbo_stream
        format.json { render :show, status: :created, location: @book }
      else
        format.html { render :new, status: :unprocessable_entity }
        format.json { render json: @book.errors, status: :unprocessable_entity }
      end
    end
  end
1

There are 1 answers

8
Alex On BEST ANSWER

You want to add data-turbo="false" to your form to disable turbo and make your form to be a regular HTML form:

<%= form_with(model: book, data: {turbo: false}) do |form| %>

data-turbo-stream allows Turbo Streams to be used with GET requests...

data-turbo="false" disables Turbo Drive on links and forms...

https://turbo.hotwired.dev/reference/attributes


Update

If you have both formats in your action, turbo_stream takes priority, but there are multiple ways to override this, for example setting format in your params:

<%= form_with model: book do |form| %>
  <%= hidden_field_tag :format, :html %>

  <%= form.submit %>
<% end %>

For more details:

https://stackoverflow.com/a/75657122/207090