Rails RABL帮助程序从索引视图发送对象

时间:2013-04-24 14:56:54

标签: ruby-on-rails ruby rabl

我有这样的index.rabl:

collection @exchangers, :root => "bank", :object_root => false

extends "exchanger_lists/show"

和show.rabl:

object @exchanger
attributes :id, :name, :address, :location_id, :latitude, :longitude, :exchanger_type_id
node(:location_name) {|exchanger_list| exchanger_list.location.name }
node(:exchanger_type_name) {"normal" }
child currencies(@exchanger)  do
  attribute :value, :direction_of_exchange_id, :exchanger_list_id
end

我的控制器是这样的:

  def index
    @exchangers = ExchangerList.all
  end
  def show
    @exchanger = ExchangerList.find(params[:id])
  end

当我在浏览器中打开我的show动作时,一切正常,我的助手被发送@exchanger对象,一切正常。但!当我调用索引动作时,我得到:

Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id

数据一切正常......

当我使用index rabl view来帮助@exchanger对象时如何发送?

1 个答案:

答案 0 :(得分:0)

HEH) 通过自己查找,将分享:

我必须写的是: root_object

object @exchanger
attributes :id, :name, :address, :location_id, :latitude, :longitude, :exchanger_type_id
node(:location_name) {|exchanger_list| exchanger_list.location.name }
node(:exchanger_type_name) {"normal" }
child currencies(root_object)  do
  attribute :value, :direction_of_exchange_id, :exchanger_list_id
end