ruby-on-railsruby-on-rails-7form-with

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?


Solution

  • 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