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

自作多情 提交于 2019-12-10 23:47:58

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!