Rails:在对象上保存字符串——语法问题?

发布于 2024-08-26 03:24:38 字数 528 浏览 4 评论 0原文

我正在尝试编写一个简单的函数来清理文件名字符串并更新对象。当我保存测试字符串时它可以工作,但是当我尝试保存我创建的字符串变量时,什么也没有发生。但是当我返回字符串时,输出似乎是正确的!我缺少什么?

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清秋悲枫 2024-09-02 03:24:38

仅当对象 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文