Hi!
i'm not using json_api adapter, so link method is not working for me.
Can you please advise good way to resolve problem with undefined url or path?
class ResourceSerializer < ApplicationSerializer
attribute :embed_code
def embed_code
"<iframe src=\"#{embed_resource_url(object)}\"></iframe>"
end
end
Try include Rails.application.routes.url_helpers
Yes, this is fastest approach, but this requires :host in default_url_options and I don't like it, because url helper method in view context returns correct url, based on request I think. I don't like this idea for json_api also.
Another solution (which works without specifying host):
class ApplicationController < ActionController::Base
serialization_scope :view_context
end
class ResourceSerializer < ApplicationSerializer
attribute :embed_code
def embed_code
"<iframe src=\"#{view_context.embed_resource_url(object)}\"></iframe>"
end
end
@attenzione you'll need to either write a custom adapter or one of the above options. I'm going to close this issue as resolved since it is squarely in userland, but please report back here what you end up doing for others who are interested.
@bf4 currently i'm using solution, what I've provided before. This way I have access to all helpers without including them in each/some serializers, more flexible. I will report if I find some other solution
Most helpful comment
Yes, this is fastest approach, but this requires
:hostindefault_url_optionsand I don't like it, because url helper method in view context returns correct url, based on request I think. I don't like this idea for json_api also.Another solution (which works without specifying host):