我在公共文件夹rails中有徽标文件。
我想发送的文件,作为网址,以便前端可以访问网址,以查看文件。
如何将文件作为url发送到我的响应json中的文件?
我有这样的序列化程序
class PostOwnerSerializer
include FastJsonapi::ObjectSerializer
attribute :avatar do |u|
simple_logo = File.open(File.join(Rails.root, 'public', 'simple.png'))
# Rails.application.routes.url_helpers.rails_blob_url(simple_logo , only_path: true)
end
end我尝试使用rails url helper,但它出错。
NoMethodError:
undefined method `signed_id' for #<File:/ahoyhu/public/simple.png> 发布于 2021-03-29 19:56:03
simple_logo = "http://localhost:3000/simple.png"因此您可以将domain作为ENV变量。例如ENV‘’HOST‘= "http://localhost:3000",将url格式化为
simple_logo = "#{ENV['HOST']}/simple.png"注意:将http://localhost:3000替换为您的域
如果您在生产环境中运行应用程序,请在production.rb中启用以下代码
config.public_file_server.enabled = true
config.serve_static_assets = truehttps://stackoverflow.com/questions/66852489
复制相似问题