Question

I'm using RABL gem, my user model has many posts

users/show.rabl

object @user
    attributes :id, :name

    child :posts do
        extends "posts/post"
    end

posts/_post.rabl

attributes :id, image: post.image.url

I want to show post's image, but i have an error: undefined local variable or method "post".

But in posts/_post.html.erb i can use this <%= post.image.url =%>

Images loaded by dragonfly gem.

How can i get this image url in json format?

Was it helpful?

Solution

If this is a child-attribute you can append it back to the root node by using glue.

Like this:

@post
attributes :id, :image_url

glue @image do
  attributes :url => :image_url
end

Or perhaps like this:

@post
attributes :id

node :image_url do |post|
  post.image.url
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top