Hey, I’m trying to create the basic example of a form loaded through turbo which loads itself when validations fails and redirect when the model is created correctly.
Most examples on the internet use extra steps which seem to be already covered with Turbo default behavior, but I haven’t been able to make it work.
I have the typical create action with the following code
def create
@question = Question.new params.require(:question).permit(:text, :answer)
if @question.save
redirect_to questions_path, status: :see_other
else
render :new, status: :unprocessable_entity
end
end
My index template have a turbo_frame_tag
to load the form
<%= turbo_frame_tag 'new', src: new_question_path do %>
<% end %>
and the new template just have a turbo_frame_tag with the form inside
<%= turbo_frame_tag 'new' do %>
... form here...
<% end %>
The validations errors are shown correctly, but when the model is created, turbo loads the “index” page (as the redirect ask to) but it’s not rendering the _top frame.
Some answers to this questions say to add the target: '_top'
property to the frame_tags, but in that case, when the creation fails the whole page is changed by the new
template (and I lost the index contents)
Others answers say to use respond_to
and use turbo_streams to replace the form and append the new content, but that feels a lot hacky and the turbo documentations says that returning the see_other
status code on redirect should do the work.
Hope the question is clear, if not, let me know so I can provide extra examples. Thanks.