【发布时间】:2018-09-07 15:11:41
【问题描述】:
我有一个使用 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 指南 (http://edgeguides.rubyonrails.org/active_storage_overview.html#linking-to-files) 中的建议使用 Rails.application.routes.url_helpers.url_for(image),但它会引发此错误:
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 的正确方法是什么?
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-5 rails-activestorage