0

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> 

1 Answer 1

1
    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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.