Ralph King
Ralph King

Reputation: 1064

Rabl and Paperclip URLs

I'm moving from jbuilder to Rabl for a search page on my site, for speed purposes. It's seemingly 50% faster without doing any other optimisation yet, which is good.

I am having trouble sending a paperclip URL through rabl to the view though.

Original Jbuilder view:

json.array!(@venues) do |venue|
  json.extract! venue, :id, :name, :longitude, :latitude, :price_range, :venue_category_id, :address, :short_description, :max_capacity

  json.venue_images venue.venue_images do |vi|
    json.url vi.image.url(:big)
    json.urlthumb vi.image.url(:thumb)
    json.id vi.id

  end
end

Current Rabl view:

object @venues
    attributes :id, :name, :longitude, :latitude, :price_range, :venue_category_id, :address, :short_description, :max_capacity

child :venue_images do
    attributes :id, :caption
        child :image do 
        attributes :url
    end

end

This is working fine and I'm getting and displaying the images, but I want to be able to use the Paperclip helper on the :url attribute, but Rabl throws a syntax error if I try :url(:thumb)

Is there a way around this?

Thanks

Upvotes: 0

Views: 175

Answers (2)

tirdadc
tirdadc

Reputation: 4713

Based on Image url in JSON (RABL, Dragonfly, Ruby on Rails)

Try:

child :venue_images do
  attributes :id, :caption
    node :image do |vi|
      vi.image.url(:thumb)
    end
end

Upvotes: 1

turbod
turbod

Reputation: 1988

I think you must add a node to the venue images.
Like this:

child :venue_images do
    attributes :id, :caption
    node :url do |image|
       o.image.url(:thumb)
    end
end

Upvotes: 1

Related Questions