refinerycms-blog
refinerycms-blog copied to clipboard
Friendly urls are not generated on save
Hi,
It seems that post's slug is not generated when save/update.
I'm using gem 'refinerycms', '~> 4.0' gem 'refinerycms-blog', git: 'https://github.com/refinery/refinerycms-blog', branch: 'master'
It might be that this one returns always false:
# If custom_url or title changes tell friendly_id to regenerate slug when
# saving record
def should_generate_new_friendly_id?
saved_change_to_attribute?(:custom_url) || saved_change_to_attribute?(:title)
end
I had forced this method to always return false and the slug/friendly_id is generated. So it might be that those attributes are not seen as saved.
I have changed to attribute_changed?(:custom_url) || attribute_changed?(:title)
and it returns correctly the state of attribute changes. I wonder if is not a Rails 5.1 issue?
thank you Ionut
I found some documentation around this: http://blog.toshima.ru/2017/04/06/saved-change-to-attribute.html which says that attribute_changed? is deprecated and that saved_change_to_attribute? is the new version.
We updated this in #480 as this version of Refinery specifically targets Rails 5.1.x
I wonder what could be going wrong?
Sorry for my late response. It might have been that the documentation of saved_change_to_attribute? was not clear. I would guess that in our case we were evaluating if an attribute was updated, but there was not any save method called yet. (in Rails 5.1+?)
I have changed with will_save_change_to_attribute - post.rb - and it seems to be working fine: will_save_change_to_attribute?(:custom_url) || will_save_change_to_attribute?(:title)
Couldn't run tests and didn't had time to figure out how can setup al refinerycms in dev mode. If you have some documentation about what do I need setup in a dev env, I will be happy to contribute.
Best Ionut
Hi Ionut, thank you! Does this guide help you? https://www.refinerycms.com/guides/contributing-to-refinery
Yes, I should have seen it :)
I'll make a PR on this issue after I will have the dev + test mode running.
Best Ionut
Hi, this little change solved friendly URL me too.
saved_change_to_attribute?(:custom_url) || saved_change_to_attribute?(:title)
to
will_save_change_to_attribute?(:custom_url) || will_save_change_to_attribute?(:title)
Hi @spotlessicode, could you provide a pull request with a test?
Hi @bricesanchez I tried, but after several hours, I couldn't set forked and cloned refinerycms-blog in local.

It doesn' work if you change something in the post, but not the title, it just goes back to the blog/post/1 url instead of the friendly one. So you have to add a space or something at the and of the title if you edit a post and save with that title changes. so better to use body as well:
will_save_change_to_attribute?(:custom_url) || will_save_change_to_attribute?(:title) || will_save_change_to_attribute?(:body)