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 %>

About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 9
- Comments: 18 (3 by maintainers)
Commits related to this issue
- Render 422 Response HTML Related to https://github.com/hotwired/turbo-rails/issues/12 Related to https://github.com/hotwired/turbo-rails/issues/34 Typically, Turbo expects each FormSubmission reques... — committed to hotwired/turbo by seanpdoyle 4 years ago
- Render 422 Response HTML Related to https://github.com/hotwired/turbo-rails/issues/12 Related to https://github.com/hotwired/turbo-rails/issues/34 Typically, Turbo expects each FormSubmission reques... — committed to hotwired/turbo by seanpdoyle 4 years ago
- Render 400-500 status response HTML Related to https://github.com/hotwired/turbo-rails/issues/12 Related to https://github.com/hotwired/turbo-rails/issues/34 Typically, Turbo expects each FormSubmis... — committed to hotwired/turbo by seanpdoyle 4 years ago
- Render 400-500 status response HTML Related to https://github.com/hotwired/turbo-rails/issues/12 Related to https://github.com/hotwired/turbo-rails/issues/34 Typically, Turbo expects each FormSubmis... — committed to hotwired/turbo by seanpdoyle 4 years ago
- Render 400-500 status response HTML Related to https://github.com/hotwired/turbo-rails/issues/12 Related to https://github.com/hotwired/turbo-rails/issues/34 Typically, Turbo expects each FormSubmis... — committed to domchristie/turbo by seanpdoyle 4 years ago
- Fixed issue with Turbo and calling 'render' when failing https://github.com/hotwired/turbo-rails/issues/12#issuecomment-754629885 — committed to modulehandbook/modulehandbook by YoussefHenna 2 years ago
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_tagto wrap the form, but normal form still got the error.Sorry to bring this up from the dead, but when I use this trick of
status: :unprocessable_entityit 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:
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 think it does.
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
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:
In controller:
And form.turbo_stream.erb
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.