首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >更新活动存储附件文件名

更新活动存储附件文件名
EN

Stack Overflow用户
提问于 2018-10-24 04:26:16
回答 1查看 3.9K关注 0票数 1

我想知道如何更新活动存储附件的文件名。

下面是我正在尝试的:

代码语言:javascript
复制
2.5.1 :052 > attachment.filename                                                                                                                                                             
 => #<ActiveStorage::Filename:0x00007fb2926cf6a0 @filename="example.pdf">
# at this point the filename is example.pdf

2.5.1 :053 > attachment.update!(filename: 'foo.pdf')                                                                                              
   (0.2ms)  BEGIN                                                        
  Patient Load (0.5ms)  SELECT  "patients".* FROM "patients" WHERE "patients"."deleted_at" IS NULL AND "patients"."id" = $1 LIMIT $2  [["id", 40861], ["LIMIT", 1]]
   (0.2ms)  COMMIT                                                                                                                                                                           
 => true
# I update the filename to "foo.pdf" and the save is evidently successful

2.5.1 :054 > attachment.reload.filename                                                                                                                                                      
  ActiveStorage::Attachment Load (0.4ms)  SELECT  "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."id" = $1 LIMIT $2  [["id", 99], ["LIMI
T", 1]]                                                                                                                                             
  ActiveStorage::Blob Load (0.3ms)  SELECT  "active_storage_blobs".* FROM "active_storage_blobs" WHERE "active_storage_blobs"."id" = $1 LIMIT $2  [["id", 99], ["LIMIT", 1]]
 => #<ActiveStorage::Filename:0x00007fb292675308 @filename="example.pdf">   
# I reload the attachment and the filename reverts to what it was before

如你所见,我正在尝试的东西不起作用。如何更改文件名?

EN

回答 1

Stack Overflow用户

发布于 2018-10-24 04:36:28

我想通了:

代码语言:javascript
复制
attachment = ActiveStorage::Attachment.find(attachment_id)
attachment.blob.update!(filename: 'new_filename.pdf')
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52957348

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档