【发布时间】:2015-11-17 11:38:37
【问题描述】:
我目前正在构建一个模型为Post 的应用程序。我正在使用paperclip gem 上传图片,一切顺利。
class Post < ActiveRecord::Base
has_attached_file :headerimage, styles: {banner => "400x300#"}
end
正如您在上面的课程中看到的那样,如果我要获得 Post 对象,我可以在我的视图中获得带有以下内容的横幅图像:
image = Post.first.headerimage(:banner)
别名
但是,在我的应用程序中,它必须具有图像属性image 指的是缩略图图像。所以,在我的模型课上,我写了
class Post < ActiveRecord::Base
has_attached_file :headerimage, styles: {banner => "400x300#"}
alias_attribute :image, :headerimage
end
这允许我通过调用以下命令来获取图像:
image = Post.first.image
这就是我想要的——但是,它从回形针中获取原始图像,所以它相当于编写以下内容:
image = Post.first.headerimage 而不是image = Post.first.headerimage(:banner)
如何设置正确的 alias_attribute 来访问回形针缩略图?我似乎无法在其他任何地方找到答案,而且我不确定回形针实际上是如何工作的。
我认为我可能在逻辑上能够做类似的事情
alias_attribute :image, :headerimage(:banner)
但这不起作用。
【问题讨论】: