如何使回形针的附件路径值不可变?

发布于 2024-10-11 21:38:46 字数 205 浏览 5 评论 0原文

每次请求路径时,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 技术交流群。

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

发布评论

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

评论(2

你在看孤独的风景 2024-10-18 21:38:46

我的一位同事想出了一个很好的解决方案。它没有无缝集成到回形针中,但它很有效。要点是:

  1. 在模型上使用“before_create”过滤器来拍摄路径中可能更改的所有值的快照

  2. 引用回形针路径插值中的快照值(而不是实际源,可能会更改)

所以你的路径定义看起来像这样:

:path => '.../:snapshotted_name/...'

并且代码看起来像这样:

before_create :snapshot_names # only set once

private

def snapshot_names
  snapshotted_name = customer.name
end

a colleague of mine came up with a good solution. it's not seamlessly integrated into paperclip but it's effective. the gist is:

  1. using a "before_create" filter on the model to take a snapshot of all the values used in the path that could change

  2. 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:

:path => '.../:snapshotted_name/...'

and the code looks something like this:

before_create :snapshot_names # only set once

private

def snapshot_names
  snapshotted_name = customer.name
end
好菇凉咱不稀罕他 2024-10-18 21:38:46

我发现了问题。

我有两种情况,原始路径依赖关系可能会改变

为什么它们会改变?这两个案例是什么?是什么阻止你想出一个永远不会改变的计划?

I found the problem.

I have two cases where the original path dependencies can change

Why will they change? What are these two cases? What is preventing you from coming up with a scheme whereby they will never change?

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