从Rabl Rails调用Rails助手 [英] Calling Rails helper from rabl Rails

查看:98
本文介绍了从Rabl Rails调用Rails助手的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从我的rabl模板中调用Rails助手,但是它不起作用.

I'm trying to call a Rails helper from my rabl template, but it's not working.

我有一个图像对象,我想通过我的api传递它.因此,图像路径应具有完整的URL(例如:http://<my url>/uploads/myimage.jpg).我想通过Rails助手添加的主机部分.

I have an image object and I want to pass its pass through my api. So then image path should have the full url (Ex: http://<my url>/uploads/myimage.jpg). The host part I want to add through a Rails helper.

#my rabl file
object @image
attributes :id, :picture_url

我想要的是类似的东西(我尝试过但是没用)

What I want is something like (I tried this but doesn't work)

#my rabl file
object @image
attributes :id, full_url(:picture_url)

#app/helpers/application_helper.rb
module ApplicationHelper
  def full_url(path)
    "#{request.host}#{path}" #something like this
  end
end

但是当我这样做时,它会完全从json对象中删除:picture_url属性

But when I do this, it completely drop the :picture_url attribute from the json object

推荐答案

我相信这可能对您有用:

I believe this might work for you:

object @image
attributes :id
node(:picture_url) { full_url(:picture_url) }

node允许您定义不直接映射到对象属性的内容.

node allows you to define things that do not directly map to object attributes.

编辑1

看来我弄错了node语法.尝试以下方法:

It looks like I got the node syntax wrong. Try this instead:

node(:picture_url) { |i| full_url(i.picture_url) }

这篇关于从Rabl Rails调用Rails助手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆