【发布时间】:2015-08-16 10:48:52
【问题描述】:
我博客上的帖子有几张图片
class Post < ActiveRecord::Base
has_many :pictures, dependent: :destroy
end
图片有一个附加的图像文件,我使用gem回形针来管理这个。
class Picture < ActiveRecord::Base
belongs_to :post
has_attached_file :image,
:path => ":rails_root/public/images/:id/:filename",
:url => "/images/:id/:filename",
styles: { medium: "300x300>", thumb: "100x100>" }
do_not_validate_attachment_file_type :image
end
我可以使用<%= image_tag picture.image.url %> 轻松创建图像标签
所以picture.image.url 返回存储图像的url。我正在尝试使用液体模板宝石来获取此网址。
我想做类似的事情
{% for picture in pictures %}
<li style="background-image:'{{picture.image.url(:medium)}}';"></li>
{% endfor %}
我猜我需要一个液体方法:url,但我没有模型来编写它(因为image 是附件,而不是模型)。有人知道我如何通过液体获取图片网址吗?
【问题讨论】:
-
液体序列化使用什么 gem,如何运行序列化?
-
在我的 gemfile 中我只有 gem "liquid" 我会使用类似
markdown = Liquid::Template.parse(text).render("pictures" => @post.pictures)的东西运行序列化
标签: ruby-on-rails ruby liquid