2012-09-20 19 views
0

我需要將json嵌入到html中,並在#322 RailsCasts中說明如何使用RABL ...在RoR 3中傳遞一個對象來渲染(:template =>「template.json.rabl」)(解決)

app/views/places/show.html.erb 
<div id="place" data-place="<%= render(template: "places/show.json.rabl") %>" > 

這裏是我的Rabl的模板

app/views/places/show.json.rabl 
object @place 
attributes :id, :name, :desc, :address 
node(:type) { |place| place.type_place.name } 

child :photos do 
    attributes :id, :desc 
    node(:url) { |photo| photo.image.url(:square) } 
end 

,這是工作正常,但我想呈現Rabl的模板中像其他視圖:

app/views/places/finder.html.erb 
<%= @places.each do |place| %> 
    <div id="place-<%= place.id %>" data-place="<%= render(template: "places/show.json.rabl") %>" > 
<% end %> 

它讓我看到一條消息錯誤

未定義的方法`type_place」的零:NilClass

Extracted source (around line #1): 

1: object @place 
2: attributes :id, :name, :desc, :address 
3: 
4: node(:type) { |place| place.type_place.name } 

從我認爲該消息的錯誤是什麼我不傳遞對象的地方模板...我嘗試了很多,我沒有得到這個。

在此先感謝和抱歉,我的英語

回答

4

您需要顯式調用Rabl::Renderer

<%= @places.each do |place| %> 
    <div id="place-<%= place.id %>" data-place="<%= Rabl::Renderer.json(place, 'places/show', view_path: 'app/views') %>" > 
<% end %> 
+0

感謝這麼多這是工作。 – figuedmundo