Comments (18)
Fixed in 0.5.3. Render your form error response with unprocessable entity (422) and Turbo will display it.
from turbo-rails.
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.
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.
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.
@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.
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.
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.
I dont think the scafold generators know to add the status: :unprocessable_entity in rails 7.01
I think it does.
from turbo-rails.
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.
Upgrading to rails 6.1 fixed this.
from turbo-rails.
Also, make sure you're using at least ruby 2.6.
from turbo-rails.
@adammiribyan I'm using ruby 2.7.2 and Rails 6.1.0.
from turbo-rails.
@nyrf what's your controller code for that form?
from turbo-rails.
@adammiribyan I used default scaffold command, rails g scaffold post title body:text
from turbo-rails.
I dont think the scafold generators know to add the status: :unprocessable_entity in rails 7.01
from turbo-rails.
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.
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.
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)
- Chrome reloads assets in the Link preload header HOT 1
- Uglifier::Error during assets:precompile HOT 2
- Turbo not streaming changes properly / window.Turbo is undefined HOT 3
- InstaClick prefetch not working with fragment cache
- Prefetch causes turbo stream to render on hover instead of on click HOT 4
- Inverted parameters in stream responses HOT 2
- unexpected routes added as part of the turbo rails gem HOT 3
- When streaming from a worker, path helpers append a domain `https://example.com` HOT 5
- Turbo Documentation need a "Broadcast" chatper
- Refresh broadcasts generated without changes
- Incompatible with Solid_Queue HOT 1
- turbo_frame_request_id safe operator in not handled correctly HOT 2
- Shouldn't broadcast refresh if streamables == [nil] HOT 1
- @npezza93 ActiveRecord::RecordNotFound in MembersController#edit_description
- FORM with an action URL containing a query param is not submitted. HOT 1
- Mixed Content Error with Turbo Drive
- OT: Attackers might be trying to steal your information from discuss.hotwire.dev HOT 2
- Turbo refresh can hijack user navigation HOT 3
- import "@hotwired/turbo-rails" errors HOT 1
- Using data-turbo-confirm on plain (non-form) buttons HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from turbo-rails.