[LRUG] Turbo - form render on error, redirect on success
Andrew Stewart
boss at airbladesoftware.com
Fri Jan 28 04:39:49 PST 2022
Thanks for the reply, Simon. That issue was one of the many I had read, along with countless posts on the Hotwire forum.
The Turbo handbook's Drive page covers Form Submissions[1] and Redirecting After a Form Submission[2]. It says it'll do the right thing if you repond to an invalid form submission with 422 and a valid one with 303. But that's on the Drive page, not the Frames page. Furthermore the Drive page goes on to talk about Streaming After a Form Submission[3], not frames.
Perhaps in this case it's better to use a stream than a frame.
I went with a frame initially so I could easily lazy-load it when the modal was opened. Later I decided instead to populate the modal with the form at page load – so I'm not getting any real benefit from the frame.
So now I have ditched the frame and changed my controller action to:
def create
thing = Thing.new thing_params
respond_to do |format|
if thing.save
format.html { redirect_to things_path, status: :see_other }
else
format.turbo_stream {
render turbo_stream: turbo_stream.replace(thing, partial: 'new', locals: {thing: thing})
}
end
end
end
And that works the way I want. It's still a bit clunky but it's ok for now. Hopefully one of the various solutions proposed on GitHub will be incorporated into Turbo before too long.
Yours,
Andy
[1] https://turbo.hotwired.dev/handbook/drive#form-submissions <https://turbo.hotwired.dev/handbook/drive#form-submissions>
[2] https://turbo.hotwired.dev/handbook/drive#redirecting-after-a-form-submission <https://turbo.hotwired.dev/handbook/drive#redirecting-after-a-form-submission>
[3] https://turbo.hotwired.dev/handbook/drive#streaming-after-a-form-submission
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lrug.org/pipermail/chat-lrug.org/attachments/20220128/72d6ae63/attachment.html>
More information about the Chat
mailing list