I have a model that uses a slug
column for to_param
. This works fine, including if the user omits it in #new
- it fails presence validation. However, if a user purposefully removes the value in #edit
, the validation properly fails but re-rendering the form fails with:
ActionView::Template::Error (No route matches {:action=>"show", :controller=>"admin/collections", :id=>""}, missing required keys: [:id]):
1: <%= form_with(model: [:admin, collection], html: { multipart: true }) do |form| %>
2: <%= render 'application/form_error', model: collection if collection.errors.any? %>
3:
4: <div class="field is-horizontal">
app/views/admin/collections/_form.html.erb:1
app/views/admin/collections/edit.html.erb:6
app/controllers/admin/collections_controller.rb:39:in `update'
Is there an elegant Rails way to prevent this error? Obviously client-side sanitisation is a good idea, but it shouldn’t be one’s only defense. IMO, a user making a mistake should never be able to trigger an exception like this.