从rabl Rails调用Rails助手

我试图从我的rabl模板中调用Rails助手,但它不起作用。

我有一个图像对象,我想通过我的api传递它。 那么图像路径应该有完整的URL(例如: http:///uploads/myimage.jpg )。 我想通过Rails助手添加的主机部分。

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

我想要的是(我尝试过但不起作用)

 #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属性

我相信这可能对你有用:

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

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

编辑1

看起来我的node语法错误了。 试试这个:

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

通过调用full_url(:picture_url)attributes获取的字符串与@image对象上的方法不对应,即@image.id返回一些内容,而@image.http://...@image.http://...有效:)。

我建议您先保留视图,然后使用控制器中的助手设置@image.picture_url的值。