turbo-rails: Error: Form responses must redirect to another location

After use turbo, got error message Error: Form responses must redirect to another location if submit form fail

post.rb

class Post < ApplicationRecord
  validates :title, presence: true, uniqueness: { case_sensitive: false }
end
  # POST /posts
  # POST /posts.json
  def create
    @post = Post.new(post_params)

    respond_to do |format|
      if @post.save
        format.html { redirect_to @post, notice: 'Post was successfully created.' }
        format.json { render :show, status: :created, location: @post }
      else
        format.html { render :new }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end
<h1>New Post</h1>

<%= render 'form', post: @post %>

<%= link_to 'Back', posts_path %>
WX20201223-014812@2x

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 9
  • Comments: 18 (3 by maintainers)

Commits related to this issue

Most upvoted comments

Fixed in 0.5.3. Render your form error response with unprocessable entity (422) and Turbo will display it.

How do I do a complete form with validation, populated form fields on validation failure, redirect on success with Turbo? I can’t find a single complete example that works. What about a mechanism to turn it off so my current forms can continue working until I find a workable solution/have time to migrate?

It works if use turbo_frame_tag to wrap the form, but normal form still got the error.

<%= turbo_frame_tag "post" do %>
  <%= render 'form', post: @post %>
<% end %>

Sorry to bring this up from the dead, but when I use this trick of status: :unprocessable_entity it kills javascript running in the application. – does anyone else have this issue?

@pbougie if you found an easy way to have forms opt-out of Turbo behavior that would be 💯 . The “default” rails way to do things that I usually see is:

  • submit a form
  • redirect on success
  • render html on error

As such, since Turbo currently requires redirects on all forms, it’s a major blocker to prevent folks switching over.

Thanks all. Other than that small snag, this Turbo thing is pretty neat, I was able to drop it into an existing rails app pretty easily! 🙌

I dont think the scafold generators know to add the status: :unprocessable_entity in rails 7.01

I think it does.

image

Hi @nyrf ,

I’m trying Rails 6.1 + gem hotwire-rails (latest version).

And I think it works this way No turbo_stream.erb files needed. It works like traditional form submission usage

  # POST /posts
  # POST /posts.json
  def create
    @post = Post.new(post_params)

    respond_to do |format|
      if @post.save
        format.html { redirect_to @post, notice: 'Post was successfully created.' }
-        format.json { render :show, status: :created, location: @post }
      else
-        format.html { render :new }
+        format.html { render :new, status: :unprocessable_entity}
-        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

Yeah, Turbo currently requires a redirect. Not compatible with responding with HTML to a POST for error handling. We’re looking into ways to support this.

Curious on how you’re dealing with forms and validations in Hey/Basecamp. Client side validations and being certain the submit will succeed and result in a redirect?

EDIT: I think I got the hang of it for a more traditional rails form validation flow. Please let me know if this is considered good practice.

In your form:

   <%= form_with model: @user, id: 'registration-form' %>

In controller:

  def create
    @user = User.new(user_params)

    respond_to do |format|
      if @user.save
        format.turbo_stream { redirect_to root_path }
      else
        format.turbo_stream { render :form }
      end
    end
  end

And form.turbo_stream.erb

<%= turbo_stream.replace 'registration-form' do %>
  <%= render partial: 'form', locals: { user: @user }, formats: [:html] %>
<% end %

Yeah, Turbo currently requires a redirect. Not compatible with responding with HTML to a POST for error handling. We’re looking into ways to support this.

Also, make sure you’re using at least ruby 2.6.

Upgrading to rails 6.1 fixed this.