You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm seeing this issue, when submitting a form for a model object which has validations defined on the base field. I've described my workaround below. I will also be submitting a PR.
Setup:
$ rails new friendly_id_debug
$ cd friendly_id_debug
$ rails g scaffold Post title:string body:text
$ rails db:migrate
ActiveRecord::RecordNotFound (can't find record with friendly id: "88c1529f-c6d9-4a4a-8b27-45f259091baa")
unit tests:
In test/models/post_test.rb add:
require"test_helper"classPostTest < ActiveSupport::TestCase# normal usetest"slug updates when title updates"do# set uppost=Post.create({title: "My First Post",body: "My Deep Thoughts"})assert_equal("my-first-post",post.slug)# executepost.update(title: "Changed My Mind")# desired behaviorassert_equal("changed-my-mind",post.slug)endend
This should be green already. Add a red test:
# edge case# validatition bug:# Expected ["my-second-post", "2d883d62-9c3e-4158-9daf-b34e655dcc3e"] to be nil.# This bug will break form behavior after an invalid submissiontest"slug unchanged when title update is invalid, in :valid? call"do# set uppost=Post.create({title: "My Second Post",body: "More Deep Thoughts"})original_slug=post.slug.dup# executepost.assign_attributes({title: ""})# set .changesrefute(post.valid?)# set .errors; falsy expected; trigger bug# desired behavior, slug not touched on failed validation for :titleassert_nil(post.changes["slug"])assert_equal(original_slug,post.slug)end
I'm seeing this issue, when submitting a form for a model object which has validations defined on the base field. I've described my workaround below. I will also be submitting a PR.
Setup:
$ rails new friendly_id_debug $ cd friendly_id_debug $ rails g scaffold Post title:string body:text $ rails db:migrate
In
app/models/post.rb
:In
db/seeds.rb
Finish:
test form validations as normal.
add
friendly_id
:in
app/models/post.rb
:And
app/controllers/posts_controller.rb
:Update existing records:
Add update slug on title change:
In
app/models/post.rb
:Go back to edit, and try submitting with a blank field.
But now open a chrome inpsector and look at the form:
Submitting error:
unit tests:
In
test/models/post_test.rb
add:This should be green already. Add a red test:
debugging:
The fix. in
app/models/post.rb
, add:The
base
field (i.e.:title
)should also be checked for errors.with refactoring:
config in initializer:
The text was updated successfully, but these errors were encountered: