使用对象值的回形针路径/url
我一直在尝试使用回形针来构建照片库/商店。一个图库有很多张照片,一张照片属于一个图库,用户可以拥有多个图库。回形针默认执行类似 /: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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查看 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.
@zetetic 的答案有点过时(博客文章来自 2008 年)回形针维基。因此,对于问题中的
user_name
,可能是这样的:@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: