Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Errors not displaying when rendering edit in Rails 7

My app is running Rails Rails 7.0.2.3

In my update controller action I have the line:

return render(:edit) unless @user_form.save

This renders the edit view on error .... but errors are not displayed.

In my edit view I am defining the form with:

form_for @user_form, url: user_path(@user_form), method: :patch do |f|

The form submits via turbo. I can see the error being added to @user_form.errors in the controller, but the instance of @user_form in the view is not changing on each form submission. If I output @user_form.inspect to the view - the id remains the same on each submission.

I have tried adding remote: false to the form_for call, but this does not seem to have an effect.

The only solution I have found is to add data: { turbo: false } to the form_for call.

Is there a better way of handling this?

like image 871
11 revs, 10 users 40% Avatar asked Oct 19 '25 04:10

11 revs, 10 users 40%


1 Answers

You need to render with status: :unprocessable_entity. I think the change in status invalidates the cache.

so change your code to:

return render(:edit, status: :unprocessable_entity) unless @user_form.save
like image 82
George Yacoub Avatar answered Oct 21 '25 17:10

George Yacoub



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!