refinerycms-blog icon indicating copy to clipboard operation
refinerycms-blog copied to clipboard

Friendly urls are not generated on save

Open imunteanu opened this issue 7 years ago • 8 comments

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

imunteanu avatar Apr 25 '18 09:04 imunteanu

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?

parndt avatar Apr 25 '18 19:04 parndt

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

imunteanu avatar May 01 '18 15:05 imunteanu

Hi Ionut, thank you! Does this guide help you? https://www.refinerycms.com/guides/contributing-to-refinery

parndt avatar May 02 '18 00:05 parndt

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

imunteanu avatar May 02 '18 05:05 imunteanu

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)

spotlessicode avatar Jul 08 '18 08:07 spotlessicode

Hi @spotlessicode, could you provide a pull request with a test?

bricesanchez avatar Jul 09 '18 11:07 bricesanchez

Hi @bricesanchez I tried, but after several hours, I couldn't set forked and cloned refinerycms-blog in local. screen shot 2018-07-09 at 17 08 08 screen shot 2018-07-09 at 17 07 01

spotlessicode avatar Jul 09 '18 15:07 spotlessicode

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)

spotlessicode avatar Jul 17 '18 14:07 spotlessicode