Active_model_serializers: Unable to use image_path in serializer

Created on 21 Jan 2013  路  9Comments  路  Source: rails-api/active_model_serializers

We are stuck trying to reference the image_path helper inside a serializer. We've tried including AssetTagHelper directly, but it chokes with an error:

undefined local variable or method `config' for #<GiftCardTypeSerializer:0x007fd9b6c84618>"

Digging into the AssetTagHelper, it seems coupled to a controller.

Is there any way to reference these assets safely? Generating my own URLs feels a bit barbaric. :)

Most helpful comment

I had the same issue with Rails 4.1.1 and active_model_serializers 0.8.1. I solved it using ActionController::Base.helpers.asset_path as suggested here: http://stackoverflow.com/a/16609815.

Hope this helps :)

All 9 comments

Hmmm. I am not 100% sure at this moment, to be honest.

I'm having this issue as well, are there any solutions to this? I tried using view_context as suggested here:
http://stackoverflow.com/questions/5943188/how-can-i-use-image-path-inside-rails-3-controller
But that didn't work well for me.

I'm having the same problem :( I've included the Sprockets::Helpers::RailsHelper inside my serializer, but I get the same error than @mattgreen...

@mrcasals I got mine to work, try including these at the top of your serializer:

include Sprockets::Helpers::RailsHelper
include Sprockets::Helpers::IsolatedHelper
include ActionView::Helpers::AssetTagHelper

Thanks for the update! /cc @morganick

@jonchay's tip worked for me :)

@jonchay thx :clap:

Giving this a close, then.

I had the same issue with Rails 4.1.1 and active_model_serializers 0.8.1. I solved it using ActionController::Base.helpers.asset_path as suggested here: http://stackoverflow.com/a/16609815.

Hope this helps :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AlexCppns picture AlexCppns  路  5Comments

kapso picture kapso  路  4Comments

djsegal picture djsegal  路  5Comments

attenzione picture attenzione  路  4Comments

PratheepV picture PratheepV  路  4Comments