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
非常感谢。
I am trying to write a simple function to clean a filename string and update the object. When I save a test string it works, but when I try to save the string variable I've created, nothing happens. But when I return the string, the output seems to be correct! What am I missing?
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
Thank you very much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
仅当对象 ID 发生更改时才会进行更新吗?我认为只有当对象本身发生变化时才更新槽是合理的。
您是否尝试过使用 gsub 而不是 gsub!,从而导致对象 ID 发生变化?
Is the update only going through if the object ID has changed? I think it is reasonable to update the slot only when the object itself has changed.
Have you ever tried to use gsub instead of gsub!, so that the object ID changes?