我在用宝石:friendly_id, babosa
。
模型产品.rb:
class Product < ActiveRecord::Base
extend FriendlyId
friendly_id :name, use: [:slugged, :finders]
has_many :tags
def normalize_friendly_id(input)
input.to_s.to_slug.normalize(transliterations: :russian).to_s
end
end
我推出了To.On鞋底,生产现有产品的蛞蝓:
Product.find_each(&:save)
检查最后一项:
Product.last
#<Product id: 4972, name: "ВА 47-63, 2P 2А (C) EKF", created_at: "2016-01-27 11:32:39", updated_at: "2016-01-27 11:32:39", slug: 04b4aa2d-2bb9-46f9-8d49-cbb9d0c3f872">
但音译是错误的我在控制台上也这么做了:
"ВА 47-63, 2P 2А (C) EKF".to_s.to_slug.normalize(transliterations: :russian).to_s
=> "va-47-63-2p-2a-c-ekf"
现在是正常结果了。我不明白这是为什么。
用同样的方法翻译标签,她收到了足够的。
型号tag.rb:
class Tag < ActiveRecord::Base
extend FriendlyId
friendly_id :name, use: [:slugged, :finders]
belongs_to :product
def normalize_friendly_id(input)
input.to_s.to_slug.normalize(transliterations: :russian).to_s
end
end
最佳答案
您可以通过替换模型上的函数来修复它:
"input.to_s.to_slug.normalize(transliterations: :russian).to_s" ->
"input.to_slug.normalize! :transliterations => [:russian]"
在我的模型上是:
def normalize_friendly_id(text)
text.to_slug.normalize! :transliterations => [:vietnamese]
end