【问题标题】:friendly_id scope unique to parent resource父资源独有的friendly_id 范围
【发布时间】:2014-03-22 04:20:18
【问题描述】:

是否可以将 slug 限定为父资源,以便两个用户可以共享同一个资源 slug?示例:

两个用户会有新闻文章“i-am-awesome”

域/joe/news/i-am-awesome 域名/sara/news/i-am-awesome

我已经为用户资源的唯一性对 slug 进行了自定义验证,但这仍然会为第二个用户生成 UUID。最终成为:

域/sara/news/i-am-awesome-UUID

【问题讨论】:

    标签: ruby-on-rails url slug friendly-id


    【解决方案1】:

    提供帮助可能为时已晚,但可以,您可以将friendly_id 限定为范围。来自documentation

    class Restaurant < ActiveRecord::Base
      extend FriendlyId
      belongs_to :city
      friendly_id :name, :use => :scoped, :scope => :city
    end
    
    class City < ActiveRecord::Base
      extend FriendlyId
      has_many :restaurants
      friendly_id :name, :use => :slugged
    end
    
    City.friendly.find("seattle").restaurants.friendly.find("joes-diner")
    City.friendly.find("chicago").restaurants.friendly.find("joes-diner")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-08
      • 1970-01-01
      • 1970-01-01
      • 2013-09-12
      • 2022-01-17
      • 2012-08-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多