【问题标题】:Friendly id in nested model嵌套模型中的友好 id
【发布时间】:2015-01-26 22:25:43
【问题描述】:

我正在使用 friendly_id 在我的 rails 应用程序上生成 URL。

我必须遵循模范组织:

广告:

class Ad < ActiveRecord::Base
 belongs_to :area
 ...

面积:

class Area < ActiveRecord::Base
 has_many :ads
 validates :name
 ...

Ad 模型中的以下函数为每个 Ad 生成 URL:

friendly_id :title_and_title, use: :slugged 

def title_and_title
 "classified ads france #{title}"
end

我想做的是在 URL 中添加 地区名称。 我怎么能这样做?

谢谢

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.2 friendly-id


    【解决方案1】:

    试试:

    def title_and_title
      self.area.name
    end
    

    获取其父级的名称。

    【讨论】:

    • def title_and_title "法国分类广告#{title} #{self.area.name}" end
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-18
    • 2015-03-11
    • 1970-01-01
    • 2016-05-15
    • 2013-05-28
    • 1970-01-01
    相关资源
    最近更新 更多