Make a url generator available in serializers #481

wants to merge 7 commits into


None yet
3 participants

This is built for 0.9, as an alternative to #480, and is an attempt to allow generation of URLs within serializers. This makes a url_generator (aliased as urls) object available from within any instance of ActiveModel::Serializer, and any associated serializers.

In a Rails app, we will automatically inject an instance of ActiveModel::Serializer::UrlGenerator when building a serializer from the ActionController. The same seam can be used to inject a url generator/helper in non-Rails apps.

@stevenharman stevenharman commented on the diff Jan 2, 2014

@@ -60,6 +61,10 @@ def serializer_for(resource)
+ def url_generator(*args)

stevenharman Jan 2, 2014

We might want to remove this facade to keep ActiveModel::Serializer ignorant of the actual ActiveModel::Serializer:UrlGenerator implementation when not running in a Rails app.

This kind of feature is desperately needed for building hypermedia APIs, and will most certainly be required for JSON API support. Any thoughts on it? Is there a different approach that would be preferred?

The tests are all passing for Rails 4+, but I something about the routing, or how we're hooking the RouteSet up in Rails 3.2 is not working as I'd expect. Anyone have any ideas what might be going wrong?

@vanstee vanstee commented on an outdated diff Apr 4, 2014

@@ -10,11 +10,13 @@
module TestHelper
Routes =
Routes.draw do
+ resource :post

vanstee Apr 4, 2014


I remember having trouble with url helpers for singular resources in Rails 3. Would switching to the plural resources :posts routes make sense here? Might fix your Rails 3 test failures.


steveklabnik commented Jul 28, 2014

Can this be rebased, please?

stevenharman added some commits Jan 1, 2014

@stevenharman stevenharman When runnin w/in Rails, make available url_helpers
This technique ties this usage to Rails, which but makes the helpers
directly accessible w/in any instance of ActiveModel::Serializer,
including any associations.
@stevenharman stevenharman Inject explict UrlGenerator into serializers
This is done automagically for Rails, but non-Rails apps can pass along
their own url generators/helpers to be used within serialzers.
@stevenharman stevenharman Allow configuring default_url_options AMS-wide
This is useful when running either outside of Rails, or in a background
job where you won't have access to the current url options for a
@stevenharman stevenharman Run load hooks to finish configuration e5bece5
@stevenharman stevenharman Make urls alias available on ArraySerializer too 82138fb
@stevenharman stevenharman Support Ruby 1.9, where Kernel.Hash doesn't exist 91fbe93
@stevenharman stevenharman Use plural resources to appease Rails 3
This also meant defining our own Model#to_param to we could correctly
serialize, rather than falling back on the default #to_s implementation.

I cannot seem to get the specs passing on Rails 3.2 - it's as if my resources :posts routes don't end up in the RouteSet used by url_for. Does anyone have an idea what I can do to fix that?

steveklabnik removed the jsonapi label Aug 22, 2014


steveklabnik commented Aug 22, 2014

I restarted the build, and it's still failing on 3.2 😢

I'm giving this a close because it would have to be re-done against the new 0-9-stable branch anyway. If you figure this out, let me know 😦 😢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment