0
我有模型 「書」,和列:title
,permalink
,authorname
,description
:從模型訪問屬性?
before_save :change_permalink
before_update :change_permalink
private
def change_permalink
self.permalink = operation_with_title(title)
end
def operation_with_title(string)
mytitle = string.downcase!
mytitle.delete!(".,?!()@#\$&*^%-+=/[]{}<>`~''\"")
new_string = []
mytitle.each_char do |c|
if translit.key? c
new_string << translit[c]
else
new_string << c
end
end
new_string.to_s
end
def translit
{
"а" => "a", "б" => "b", "в" => "v",
"г" => "g", "д" => "d", "е" => "e",
"ё" => "jo", "ж" => "zh", "з" => "z",
"и" => "i", "й" => "ij", "к" => "k",
"л" => "l", "м" => "m", "н" => "n",
"о" => "o", "п" => "p", "р" => "r",
"с" => "s", "т" => "t", "у" => "u",
"ф" => "f", "х" => "h", "ц" => "c",
"ч" => "ch", "ш" => "sh", "щ" => "xh",
"ь" => "", "ы" => "y", "ъ" => "",
"э" => "je", "ю" => "ju", "я" => "ja",
" " => "_"
}
end
我想有機會獲得title
,進行更改,並添加到permalink
。
但我不知道如何在我的方法中訪問title
。
給我們一個代碼片,你試過了什麼 –
http://paste.ubuntu.com/738020/ –