I am trying to dynamically render form errors using a turbo-frame. This works great when there is an error and correctly to populates the form with the submitted params, but when the form submit is successful the redirect doesn't work.
Here is the code I tried:
app/views/orders/show.html.erb
...
<%= render "order_review_form", locals: {order: @order} %>
...
app/views/_order_review_form.html.erb
<%= turbo_frame_tag "order_review_form" do %>
<% if local_assigns[:error] %>
<p class="alert alert-danger alert-dismissible fade show">
<%= sanitize(local_assigns[:error]) %>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</p>
<% end %>
<%= form_with(model: Review.new, url: order_review_path(local_assigns[:order])) do |f| %>
<div>
<%= f.label :comment %>
<%= f.text_field :comment, value: params[:comment] %>
</div>
<%= submit_tag "Submit review", class: "btn btn-sm btn-success" %>
<% end %>
<% end %>
app/controllers/order_reviews_controller.rb
...
def create
OrderReviewCreator.call(@order, params)
redirect_to(order_path(@order), status: :see_other) # this doesn't work, the page doesn't update
rescue AppError => e
render(
"_order_review_form",
locals: {
order: @order,
error: e.detail,
},
status: :unprocessable_entity,
)
end
...