我有这样的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 do
attribute :value, :direction_of_exchange_id, :exchanger_list_id
end
我的控制器是这样的:
def index
@exchangers = ExchangerList.all
end
def show
@exchanger = ExchangerList.find(params[:id])
@currency_list = CurrencyList.all
@currencies = []
@currency_list.each do |c|
@currencies << CurrencyValue.find(:all, :conditions => {:currency_list_id => c.id, :exchanger_list_id => @exchanger.id}, :order => :updated_at).last(2)
end
@currencies.flatten!
end
如果我在浏览器显示方法中调用,我会看到孩子@currencies和它的数据,但如果我调用索引我看到所有(也是我看到节点)但是孩子我没有看到......出了什么问题?我做得不好?
答案 0 :(得分:1)
您的体系结构有点混乱,因为在show动作中,当您在索引模板中呈现show时,您不仅会显示@exchanger
,还会显示@currencies
的完整列表nil。总的来说,我建议你考虑整个应用程序架构。
当我应该为您提供一个简单的解决方案时,我会将show动作中的@currencies代码提取到app / helpers / currency_helper.rb中的helper方法中,并从show模板中访问它。
module CurrenciesHelper
def currencies(exchanger)
currencies = CurrencyList.all.map do |c|
CurrencyValue.find(:all, :conditions => {:currency_list_id => c.id, :exchanger_list_id => exchanger.id}, :order => :updated_at).last(2)
end
currencies.flatten!
end
end
顺便说一句,我将each
方法替换为map
,因为在这种情况下它更适合。
将展示模板中的货币部分更改为
child currencies(@exchanger) do
attribute :value, :direction_of_exchange_id, :exchanger_list_id
end