-
Notifications
You must be signed in to change notification settings - Fork 331
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
Error: Form responses must redirect to another location #12
Comments
Upgrading to rails 6.1 fixed this. |
Also, make sure you're using at least ruby 2.6. |
@adammiribyan I'm using ruby 2.7.2 and Rails 6.1.0. |
It works if use <%= turbo_frame_tag "post" do %>
<%= render 'form', post: @post %>
<% end %> |
@nyrf what's your controller code for that form? |
@adammiribyan I used default scaffold command, |
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. |
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? |
Related to hotwired/turbo-rails#12 Related to hotwired/turbo-rails#34 Typically, Turbo expects each FormSubmission request to result in a redirect to a new Location. When a FormSubmission request fails with an [unprocessable entity][422] response, render the response HTML. This commit brings the same behavior to `<turbo-frame>` elements. [422]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/422
Related to hotwired/turbo-rails#12 Related to hotwired/turbo-rails#34 Typically, Turbo expects each FormSubmission request to result in a redirect to a new Location. When a FormSubmission request fails with an [unprocessable entity][422] response, render the response HTML. This commit brings the same behavior to `<turbo-frame>` elements. [422]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/422
@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! 🙌 |
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 %
|
Related to hotwired/turbo-rails#12 Related to hotwired/turbo-rails#34 Typically, Turbo expects each FormSubmission request to result in a redirect to a new Location. When a FormSubmission request fails with an HTTP Status code between 400-499 or 500-599 (e.g. [unprocessable entity][422]), render the response HTML. This commit brings the same behavior to `<turbo-frame>` elements. [422]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/422
Related to hotwired/turbo-rails#12 Related to hotwired/turbo-rails#34 Typically, Turbo expects each FormSubmission request to result in a redirect to a new Location. When a FormSubmission request fails with an HTTP Status code between 400-499 or 500-599 (e.g. [unprocessable entity][422]), render the response HTML. This commit brings the same behavior to `<turbo-frame>` elements. [422]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/422
Related to hotwired/turbo-rails#12 Related to hotwired/turbo-rails#34 Typically, Turbo expects each FormSubmission request to result in a redirect to a new Location. When a FormSubmission request fails with an HTTP Status code between 400-499 or 500-599 (e.g. [unprocessable entity][422]), render the response HTML. This commit brings the same behavior to `<turbo-frame>` elements. [422]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/422
Fixed in 0.5.3. Render your form error response with unprocessable entity (422) and Turbo will display it. |
Hi @nyrf , I'm trying Rails 6.1 + gem hotwire-rails (latest version). And I think it works this way # 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 |
Sorry to bring this up from the dead, but when I use this trick of |
I dont think the scafold generators know to add the status: :unprocessable_entity in rails 7.01 |
@dhh Any news on supporting TURBO_STREAM responses like |
@lapser you can already do this today, just add the format.turbo_stream { render turbo_stream: turbo_stream.append_all("body", "Form submission failed"), status: :unprocessable_entity } |
@jakemumu I just ran into this today and realized it wasn't a Turbo error, but rather the 3rd party library I was using. It only initializes on page load, but we're replacing parts of the dom. So I had to wrap it with a Stimulus controller and manually run the code (a Datepicker in this instance). I imagine you are likely hitting the same issue. |
This is true. Currently not having the most pleasant time working with forms in rails, but I think it is only due to not 100% beginner friendly documentation and guides. (Once I get it to work, I definitely will write some guides on how to work with that...) |
After use turbo, got error message
Error: Form responses must redirect to another location
if submit form failpost.rb
The text was updated successfully, but these errors were encountered: