I wanna edit entities URLs from DB by adding for them. How I can use root_url
or root_path
in a serializer?
Something like this:
class TrackSerializer < ActiveModel::Serializer
attributes :id, :title, :mp3, :ogg
has_one :promo_album
def mp3
root_url + object.mp3
end
def ogg
root_url + object.ogg
end
end
But this doesn't work.
The issue is that Rails.application.routes.url_helpers isn't included by default here. If you replace
root_url
with
Rails.application.routes.url_helpers.root_url
you should get the results you're looking for.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With