0

我需要将 json 嵌入到 html 中,并且在#322 RailsCasts中说明了使用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 %>

它显示下一条消息错误

nil:NilClass 的未定义方法“type_place”

Extracted source (around line #1):

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

从消息中我认为错误是我没有将对象位置传递给模板......我尝试了很多但我没有得到这个。

提前谢谢,对不起我的英语

4

1 回答 1

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 %>
于 2012-09-20T01:25:53.220 回答