Search code examples
ruby-on-railsruby-on-rails-4active-model-serializers

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.


Solution

  • I just forgot to define root param in the routes.rb. include Rails.application.routes.url_helpers helps me to get root_path in the serializer.