2010-03-20 72 views
0

我想寫一個簡單的函數來清理文件名字符串並更新對象。當我保存一個測試字符串時,它會起作用,但是當我嘗試保存我創建的字符串變量時,什麼都不會發生。但是當我返回字符串時,輸出似乎是正確的!我錯過了什麼?Rails:在一個對象上保存一個字符串 - 語法問題?

def clean_filename  
    clean_name = filename 
    clean_name.gsub! /^.*(\\|\/)/, '' 
    clean_name.gsub! /[^A-Za-z0-9\.\-]/, '_' 
    clean_name.gsub!(/\_+/, ' ') 
    #update_attribute(:filename, "test") #<-- correctly sets filename to test 
    #update_attribute(:filename, clean_name) #<-- no effect????? WTF 
    #return clean_name <-- seems to returns the correct string 
end 

非常感謝。

+0

哪裏clean_filename被調用函數? – Laz 2010-03-20 19:32:45

+0

我從控制檯調用它進行測試;它附加到文檔對象。如Document.find(1).clean_filename – pendevere 2010-03-20 19:37:01

+0

沒有這個系統上軌安裝,所以無法測試,嘗試write_attribute。 – Laz 2010-03-20 20:07:16

回答

-1

是更新僅經歷,如果對象ID發生了變化?我認爲只有當對象本身發生變化時才更新插槽是合理的。

你曾經嘗試過使用,而不是GSUB GSUB!這樣對象ID變化?

相關問題