2013-02-11 62 views
0

這是我的模型無法生成URL

class Tourisme < ActiveRecord::Base 
    has_friendly_id :title, :approximate_ascii => true 

    . 
    . 

end 

當標題包含"?""/",我想生成一個網址:

admin_tourisme_path(tourisme) 

我得到一個錯誤

admin_tourisme_url failed to generate from {:action=>"show", :id=>#<Tourisme id: 14, title: "title with ?", description:............. 

我該如何解決這個問題

回答

0

所以閱讀friendly_id文檔(http://rubydoc.info/github/norman/friendly_id/master/file/WhatsNew.md)我發現2個解決方案:

1 - 使用#normalize_friendly_id模型: 從DOC:

再見Babosa

Babosa是FriendlyId 3的猛擊庫。

FriendlyId 4,因爲它最重要的部分已經被接受爲主動支持3

但是默認不使用它,Babosa仍然是有用的,例如,對於慣用音譯西裏爾字母(或其他語言)字符串轉換爲ASCII。這很容易,包括 - 只需重寫#normalize_friendly_id在你的模型:

class MyModel < ActiveRecord::Base 
    ... 

    def normalize_friendly_id(text) 
    text.to_slug.normalize! :transliterations => :russian 
    end 
end 

2 - 使用猛擊型號:http://rubydoc.info/github/norman/friendly_id/master/FriendlyId/Slugged