Rails 4 Friendly Id Slug Not Updating

前端 未结 2 1751
情话喂你
情话喂你 2021-02-12 07:13

I\'m using the following:

gem \'friendly_id\', github: \'FriendlyId/friendly_id\', branch: \'master\'

I am creating an Article section on my Ra

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-12 07:59

    I have this issues and just want to point out what I've noticed.

    if you only do as in docs

    class Post < ActiveRecord::Base
     extend FriendlyId
     friendly_id :title, use: :slugged
    end
    

    and then run Post.find_each(&:save) - slug is gonna get updated...

    However in my case, I also have these in my model

    class Post < ActiveRecord::Base
     extend FriendlyId
     friendly_id :title, use: :slugged
    
     def normalize_friendly_id(text)
       text.to_slug.normalize(transliterations: :russian).to_s
     end
    
     def should_generate_new_friendly_id?
       title_changed?
     end
    end
    

    with the code above it won't do anything when you run Post.find_each(&:save) I assume since your title doesn't change. (first method handles russian language)

    so when working with the first model all worked great, but then when I copied ready code to next model I wanted to slugify, I run into some issues. Hope it helps someone.

提交回复
热议问题