fast_jsonapi icon indicating copy to clipboard operation
fast_jsonapi copied to clipboard

NoMethodError: undefined method 'abc_path' for UserSerializer:Class

Open loingh opened this issue 7 years ago • 4 comments

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?

loingh avatar Nov 08 '18 04:11 loingh

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 avatar Dec 07 '18 01:12 flynn1982

@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...

loingh avatar Dec 07 '18 03:12 loingh

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

ChrisHampel avatar Dec 07 '18 03:12 ChrisHampel

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

andyw8 avatar Dec 31 '18 19:12 andyw8