在RoR 3中将对象传递给渲染(:template =>“template.json.rabl”)

时间:2012-09-20 01:17:37

标签: ruby-on-rails json rabl

我需要将json嵌入到html中并在#322 RailsCasts中说明使用RABL进行操作...

app/views/places/show.html.erb
<div id="place" data-place="<%= render(template: "places/show.json.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 %>

它显示了下一条消息错误

  

nil的未定义方法`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 }

从消息中我认为错误是我没有将对象放置到模板中...我尝试了很多而且我没有得到这个。

提前致谢,对不起我的英文

1 个答案:

答案 0 :(得分: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 %>
相关问题