我试图通过包含ActiveView :: Helpers在我的模型序列化器输出中包含一个图像资源管道URL:
class PostSerializer < ActiveModel::Serializer include ActiveView::Helpers attributes :post_image def post_image image_path "posts/#{object.id}" end end
结果是/images/posts/{id}
而不是资产管道路径的有效路径,即./assets/images/posts/{id}
.如何在序列化程序输出中包含有效的资产管道路径?
也许这可以工作:
def post_image _helpers = ActionController::Base.helpers _helpers.image_url "posts/#{object.id}" end