Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use `root_url` in the ActiveModel::Serializer?

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.

like image 628
lancedikson Avatar asked Jan 18 '16 19:01

lancedikson


Video Answer


1 Answers

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.

like image 185
Lockyy Avatar answered Oct 17 '22 01:10

Lockyy