在 rails 中如何将回形针 url 变为 json?
how do you get paperclip url to json in rails?
我知道 other questions pertaining to this subject, but none seem to help. I want the paperclip image url passed to json so I can render it in a reactjs component. I obviously cannot use Rails' image_tag 助手。
我已经在我的项目模型中定义了这个方法
def image_url
image.url(:thumb)
end
这在我的控制器中
def index
@items = Item.all
render :json => @items.to_json(:methods => [:image_url])
end
但字面意思就是用 json 替换呈现的页面。我该怎么办?专门为图像创建迁移和模型验证没有意义 url.
只需要格式化 html。
respond_to do |format|
format.html
format.json { render :json => @items.to_json(:methods => [:image_url]) }
end
仍然没有解决我试图在 reactjs 组件中使用该方法的部分,但这是另一个问题。
我知道 other questions pertaining to this subject, but none seem to help. I want the paperclip image url passed to json so I can render it in a reactjs component. I obviously cannot use Rails' image_tag 助手。
我已经在我的项目模型中定义了这个方法
def image_url
image.url(:thumb)
end
这在我的控制器中
def index
@items = Item.all
render :json => @items.to_json(:methods => [:image_url])
end
但字面意思就是用 json 替换呈现的页面。我该怎么办?专门为图像创建迁移和模型验证没有意义 url.
只需要格式化 html。
respond_to do |format|
format.html
format.json { render :json => @items.to_json(:methods => [:image_url]) }
end
仍然没有解决我试图在 reactjs 组件中使用该方法的部分,但这是另一个问题。