Skip to content
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

Closed
nyrf opened this issue Dec 22, 2020 · 19 comments
Closed

Error: Form responses must redirect to another location #12

nyrf opened this issue Dec 22, 2020 · 19 comments

Comments

@nyrf
Copy link

nyrf commented Dec 22, 2020

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

@adammiribyan
Copy link

Upgrading to rails 6.1 fixed this.

@adammiribyan
Copy link

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

@nyrf
Copy link
Author

nyrf commented Dec 23, 2020

@adammiribyan I'm using ruby 2.7.2 and Rails 6.1.0.

@nyrf
Copy link
Author

nyrf commented Dec 23, 2020

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

@adammiribyan
Copy link

@nyrf what's your controller code for that form?

@nyrf
Copy link
Author

nyrf commented Dec 23, 2020

@adammiribyan I used default scaffold command, rails g scaffold post title body:text

@dhh
Copy link
Member

dhh commented Dec 23, 2020

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.

@pbougie
Copy link

pbougie commented Dec 24, 2020

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?

seanpdoyle added a commit to hotwired/turbo that referenced this issue Dec 26, 2020
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
seanpdoyle added a commit to hotwired/turbo that referenced this issue Dec 26, 2020
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
@dylanjha
Copy link

@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! 🙌

@mhfs
Copy link

mhfs commented Dec 26, 2020

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 %

seanpdoyle added a commit to hotwired/turbo that referenced this issue Dec 30, 2020
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
seanpdoyle added a commit to hotwired/turbo that referenced this issue Dec 30, 2020
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
domchristie pushed a commit to domchristie/turbo that referenced this issue Jan 3, 2021
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
@dhh
Copy link
Member

dhh commented Jan 5, 2021

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

@charlietag
Copy link

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

@jakemumu
Copy link

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?

@asecondwill
Copy link

asecondwill commented Jan 12, 2022

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

@charlietag
Copy link

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

I think it does.

image

YoussefHenna added a commit to modulehandbook/modulehandbook that referenced this issue Dec 12, 2022
@lapser
Copy link

lapser commented Apr 6, 2023

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.

@dhh Any news on supporting TURBO_STREAM responses like render(html: "Support HTML", ...)?

@marcoroth
Copy link
Member

marcoroth commented Apr 9, 2023

dhh Any news on supporting TURBO_STREAM responses like render(html: "Support HTML", ...)?

@lapser you can already do this today, just add the turbo_stream format to your respond_to block and add the status: :unprocessable_entity:

format.turbo_stream { render turbo_stream: turbo_stream.append_all("body", "Form submission failed"), status: :unprocessable_entity }

@topherfangio
Copy link

topherfangio commented Apr 14, 2023

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?

@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.

@lpknv
Copy link

lpknv commented Oct 15, 2024

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?

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...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests