我有一个使用 ActiveStorage (Rails 5.2.0.rc2) 的简单模型,模型如下所示:
class Vacancy < ApplicationRecord
has_one_attached :image
validates_presence_of :title
def to_builder
Jbuilder.new do |vacancy|
vacancy.call(self, :id, :title, :description, :created_at, :updated_at)
vacancy.image do
vacancy.url image.attached? ? Rails.application.routes.url_helpers.url_for(image) : nil
end
end
end
end
然后在to_builder
我想显示图像的永久 URL 的方法中,我正在尝试Rails.application.routes.url_helpers.url_for(image)
按照 rails 指南(http://edgeguides.rubyonrails.org/active_storage_overview.html#linking-to-files)中的建议,但它会引发这个错误:
Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true
在我的应用程序中,我已经有了该default_url_options[:host]
集合,但它不起作用,甚至写入url_for(image, host: 'www.example.com')
或url_for(image, only_path: true)
不起作用,因为它引发了另一个错误:wrong number of arguments (given 2, expected 1)
使用 activestorage 在模型范围内显示永久 URL 的正确方法是什么?