Dedy Puji
Dedy Puji

Reputation: 625

send public file as url in json response rails

i have logo file in public folder rails.

i want to send the file, as url, so the front end can access the url to view the file.

How to send the file as url to the file in my response json?

i have serializer like this

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

i try for using rails url helpers but it error

NoMethodError:
undefined method `signed_id' for #<File:/ahoyhu/public/simple.png> 

Upvotes: 0

Views: 377

Answers (1)

honey
honey

Reputation: 1077

    simple_logo = "http://localhost:3000/simple.png"

So you can take domain into ENV variable. For example ENV['HOST'] = "http://localhost:3000", form the url as

    simple_logo = "#{ENV['HOST']}/simple.png"

Note: Replace http://localhost:3000 with your domain

If you are running your app in production enable following code in production.rb

    config.public_file_server.enabled = true

    config.serve_static_assets = true

Upvotes: 1

Related Questions