【问题标题】:passing an object to render( :template => "template.json.rabl") in RoR 3在 RoR 3 中将对象传递给渲染(:template => "template.json.rabl")
【发布时间】:2012-09-20 01:17:37
【问题描述】:

我需要将 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 }

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

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

【问题讨论】:

    标签: ruby-on-rails json rabl


    【解决方案1】:

    你需要显式调用Rabl::Renderer

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

    【讨论】:

      猜你喜欢
      • 2018-07-02
      • 2017-08-30
      • 1970-01-01
      • 2012-12-03
      • 1970-01-01
      • 2018-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多