【问题标题】:Rails 4 JSON render errorRails 4 JSON渲染错误
【发布时间】:2015-01-21 09:38:09
【问题描述】:

http://guides.rubyonrails.org/layouts_and_rendering.html#using-render 2.2.9 渲染 JSON 说可以在视图中渲染 JSON 如下:

render json: @product

我无法让它工作。在 index.html.erb 我有:

render json: @trees

(@trees 被定义为 Tree.all,其中包含一个 Ancestry 树,FWIW - 据我所知,与问题无关。)

我在该行收到以下错误:

你调用了 render 但没有给出任何 :partial、:template、 :inline、:file、:plain、:text 或 :body 选项。

如果我将该行代码更改为:

render json: {text:"hello world"}

我做错了什么?

【问题讨论】:

    标签: ruby-on-rails json ruby-on-rails-4 ancestry


    【解决方案1】:

    这种形式的render 调用应该进入控制器。如果您真的想在视图中呈现 JSON,<%= @trees.to_json %> 应该可以解决问题。

    【讨论】:

    • 感谢您的快速回复。从文档中不清楚,但我现在根据您的建议重读时看到了这一点。
    猜你喜欢
    • 2013-07-24
    • 2013-09-08
    • 2014-12-20
    • 1970-01-01
    • 1970-01-01
    • 2016-11-12
    • 1970-01-01
    • 2017-05-07
    • 1970-01-01
    相关资源
    最近更新 更多