Hey all,
I’ve been integrating turbo-rails into my Rails application and things have been moving along just fine.
I’m running into an issue specifically with a form that’s submitted with valid data. The form is supposed to redirect away from /projects/new
and over to the “show” page such as projects/1
.
I’ve actually recreated the issue with the example repo below. It contains the most bare-bones reaction of the behavior. No models, data, etc.
If you look at projects_controller.rb
, you’ll see:
class ProjectsController < ApplicationController
def new
end
def create
# The official docs mention a 303 as the status code to use.
# Neither of the redirect_to calls below work.
redirect_to project_path(1)
# redirect_to project_path(1), status: 303
end
def show
end
end
And the form rendered by the “new” action is:
<h1>Projects#new</h1>
<p>Find me in app/views/projects/new.html.erb</p>
<%= turbo_frame_tag "new_project" do %>
<%= form_with url: projects_path, method: :post do |f| %>
<%= f.submit "Submit" %>
<% end %>
<% end %>
So I have my “new_project” turbo frame which is great. It lets me re-render just that frame when a user submits a form with errors and those errors need to be show.
Now I’ve removed all that logic from create
. All that happens is create
redirects over to project_path(1)
just to demonstrate the behavior. So I would expect that a form submission would trigger a fetch
request from the client to fetch the “show” page and render it. That’s documented here in the official guide.
If you visit /projects/new
and click “Submit”, you’ll see the request is made. The screenshot below shows that form submission and the subsequent fetching of /projects/1
. You can see that here:
The problem though is that the response from /projects/1
is not rendered and the console shows an error.
Now the error message is correct. There is no turbo-frame
element with the new_project
id in the response. It’s a completely different page without the form. It has no turbo-frame
elements.
I’m not sure what the solution is here. I’ve read up on (and played around with) setting the target to _top
to break out of the frame. That’s not what I want as I want the frame behavior when users submit the form with invalid data.
Any thoughts on this? Thanks for your time. The forms here have been valuable over the last week.
- Andrew