小编lan*_*son的帖子

如何在ActiveModel :: Serializer中使用`root_url`?

我想通过为数据库添加实体URL来进行编辑。如何在序列化器中使用root_url或使用root_path

像这样:

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
Run Code Online (Sandbox Code Playgroud)

但这是行不通的。

ruby-on-rails active-model-serializers ruby-on-rails-4

5
推荐指数
2
解决办法
2346
查看次数