NoMethodError: undefined method 'abc_path' for UserSerializer:Class
I inclue the routes helper in UserSerializer file like this:
include ABC::Engine.routes.url_helpers
but when i use link method in UserSerializer model, like this:
link :custom_url do |object| "#{api_user_profile_path(object.id)}" end
i got error NoMethodError: undefined method api_user_profile_path for ABC::UserSerializer:Class but it run ok on active_model_serializer
Anybody have a solution for this?
I'm having the same issue when trying to use Rails.application.routes.url_helpers within the model.
Does anyone know a solution to this? I'd like to be able to use url_helpers in the model to help generate links.
@flynn1982 i have to wrote a new method to define links, also you can use: Rails.application.routes.url_helpers.abc_path, but it's not good practice...
I have been doing this in my serializers:
class << self
delegate :abc_url, :def_url, :ghi_url, to: :'Rails.application.routes.url_helpers'
end
with that you can call those helpers
I've defined this in a BaseSerializer class which all my serializers inherit from:
class BaseSerializer
class << self
delegate :url_helpers, to: :'Rails.application.routes'
end
end
then in my serializer I call it something like:
link :self do |object|
url_helpers.widget_path(object)
end