-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use persisted value for permalink field if there are unpersisted changes #2
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like a quite obscure bug - just found a little AR magic to make it even easier...
field_name = permalink_options[:with] | ||
|
||
if self.send("#{field_name}_changed?") | ||
self.send("#{field_name}_was") | ||
else | ||
self.send(field_name) | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
def persisted_permalink_field | |
field_name = permalink_options[:with] | |
if self.send("#{field_name}_changed?") | |
self.send("#{field_name}_was") | |
else | |
self.send(field_name) | |
end | |
end | |
end |
elsif(!permalink_options.empty?) | ||
"#{original_to_param}-#{self.send(permalink_options[:with])}" | ||
"#{original_to_param}-#{persisted_permalink_field}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apparently there is attribute_in_database
which already does this
"#{original_to_param}-#{persisted_permalink_field}" | |
"#{original_to_param}-#{attribute_in_database(permalink_options[:with])}" |
Problem
When saving a record fails due to a validation on a field used for a permalink and you send the user back to a form that is built with a pathhelper using said permalink functionality, the permalink is built with the in memory value instead of the persisted value and throws a routing error
Solution:
Always refer to the persisted value for building the permalink