使用对象值的回形针路径/url

发布于 2024-10-07 08:00:24 字数 349 浏览 0 评论 0原文

我一直在尝试使用回形针来构建照片库/商店。一个图库有很多张照片,一张照片属于一个图库,用户可以拥有多个图库。回形针默认执行类似 /:class/:style/:basename.:extension 的操作。但是,通过画廊设置,我宁愿拥有类似 /:class/:user_name/:gallery_name/:styles/:basename.:extension 的内容。我还没有找到一种方法来访问对象中的变量以便动态创建这些存储位置。

有什么办法可以做到这一点吗?

我尝试在路径中使用#{variable},但这不起作用。这些照片对象是使用 @gallery.photos.build 创建的,因此 gallery_id 应该已经具有可访问的值。

I've been playing around with using Paperclip to build a photo gallery/store. A Gallery has many Photos, and a Photo belongs to a Gallery, and Users can have many Galleries. The paperclip defaults do something like /:class/:style/:basename.:extension. However, with a gallery setup, I'd much rather have something like /:class/:user_name/:gallery_name/:styles/:basename.:extension. I haven't yet found a way to access variables in an object in order to dynamically create these storage locations.

Is there any way of doing this?

I've tried using #{variable} in the path, but that doesn't work. These photo objects are being created using @gallery.photos.build, so the gallery_id should already have a value that's accessible.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

森林迷了鹿 2024-10-14 08:00:24

查看 Thoughtbot.com 上的提示和更新部分。它讨论了如何将您自己的插值变量添加到路径/url 中。

Take a look at the tips and updates section on Thoughtbot.com. It discusses how to add your own interpolated variables into the path/url.

梦里南柯 2024-10-14 08:00:24

@zetetic 的答案有点过时(博客文章来自 2008 年)回形针维基。因此,对于问题中的 user_name ,可能是这样的:

# interpolate in paperclip
Paperclip.interpolates :user_name  do |attachment, style|
  attachment.instance.gallery.user.name
end

@zetetic's answer is a bit dated (the blog post is from 2008) The current (2015) way to create custom interpolations is described in the paperclip wiki. So for the user_name in the question, probably something like this:

# interpolate in paperclip
Paperclip.interpolates :user_name  do |attachment, style|
  attachment.instance.gallery.user.name
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文