Giter Site home page Giter Site logo

Comments (18)

dhh avatar dhh commented on August 10, 2024 60

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

from turbo-rails.

pbougie avatar pbougie commented on August 10, 2024 32

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?

from turbo-rails.

nyrf avatar nyrf commented on August 10, 2024 21

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

from turbo-rails.

jakemumu avatar jakemumu commented on August 10, 2024 13

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?

from turbo-rails.

dylanjha avatar dylanjha commented on August 10, 2024 7

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

from turbo-rails.

mhfs avatar mhfs commented on August 10, 2024 4

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 %

from turbo-rails.

charlietag avatar charlietag commented on August 10, 2024 4

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

from turbo-rails.

charlietag avatar charlietag commented on August 10, 2024 4

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

I think it does.

image

from turbo-rails.

dhh avatar dhh commented on August 10, 2024 3

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.

from turbo-rails.

adammiribyan avatar adammiribyan commented on August 10, 2024 1

Upgrading to rails 6.1 fixed this.

from turbo-rails.

adammiribyan avatar adammiribyan commented on August 10, 2024 1

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

from turbo-rails.

nyrf avatar nyrf commented on August 10, 2024

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

from turbo-rails.

adammiribyan avatar adammiribyan commented on August 10, 2024

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

from turbo-rails.

nyrf avatar nyrf commented on August 10, 2024

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

from turbo-rails.

asecondwill avatar asecondwill commented on August 10, 2024

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

from turbo-rails.

lapser avatar lapser commented on August 10, 2024

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", ...)?

from turbo-rails.

marcoroth avatar marcoroth commented on August 10, 2024

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 }

from turbo-rails.

topherfangio avatar topherfangio commented on August 10, 2024

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.

from turbo-rails.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.