目前我需要使用resource :id来加载页面,但我认为默认情况下显示友好的id :slug将是一个很好的尝试。我已经在使用norman/friendly-id了
如果我访问
app.example.com/houses/1我想让地址栏告诉我
app.example.com/houses/128-prinsep-st-singapore-1friendly-id已经支持这个了吗?或者什么是做这件事的好方法?
发布于 2018-03-27 13:28:50
FriendlyId提供了"slug candidates“功能,使您可以指定要使用的备用slug在您要使用的slug已被占用的情况下使用。例如:
class Example < ActiveRecord::Base
extend FriendlyId
friendly_id :slug_candidates, use: :slugged
# Try building a slug based on the following fields in
# increasing order of specificity.
def slug_candidates
[
:name,
[:name, :city],
[:name, :street, :city],
[:name, :street_number, :street, :city]
]
end
end
e1 = Example.create! name: 'Plaza Diner', city: 'New Paltz'
e2 = Example.create! name: 'Plaza Diner', city: 'Kingston'
e1.friendly_id #=> 'plaza-diner'
e2.friendly_id #=> 'plaza-diner-kingston'希望这对你有用。
https://stackoverflow.com/questions/49503365
复制相似问题