问题
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?
回答1:
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
来源:https://stackoverflow.com/questions/14941977/image-url-in-json-rabl-dragonfly-ruby-on-rails