如何使回形针的附件路径值不可变?
每次请求路径时,Rails 插件 Paperclip 都会根据一些动态的用户定义规则来插入附件路径。这允许您输入姓名/ID/等。进入你的附件路径。
但是,我有两种情况,原始路径依赖项可能会发生变化,当它们发生变化时,我再也找不到附件了,因为插值路径现在指向错误的位置。
我需要在保存文件时仅对路径进行插值一次,然后无论插值依赖项如何变化,该路径都会保留并返回那里。
The Rails plug-in Paperclip interpolates the attachment path based on some dynamic user-defined rules each time the path is requested. This allows you to put names/IDs/etc. into the paths of your attachments.
However, I have two cases where the original path dependencies can change, and when they do I can no longer find the attachment anymore, because the interpolated path now points to the wrong place.
I need the path to be interpolated just once, when the file is saved, then that path preserved and returned there after regardless of the interpolation dependencies changing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的一位同事想出了一个很好的解决方案。它没有无缝集成到回形针中,但它很有效。要点是:
在模型上使用“before_create”过滤器来拍摄路径中可能更改的所有值的快照
引用回形针路径插值中的快照值(而不是实际源,可能会更改)
所以你的路径定义看起来像这样:
并且代码看起来像这样:
a colleague of mine came up with a good solution. it's not seamlessly integrated into paperclip but it's effective. the gist is:
using a "before_create" filter on the model to take a snapshot of all the values used in the path that could change
referencing those snapshotted values from the paperclip path interpolation (as opposed to the actual source, which could change)
so your path definition looks something like this:
and the code looks something like this:
我发现了问题。
为什么它们会改变?这两个案例是什么?是什么阻止你想出一个永远不会改变的计划?
I found the problem.
Why will they change? What are these two cases? What is preventing you from coming up with a scheme whereby they will never change?