[英]Rails 4 JSON render error
http://guides.rubyonrails.org/layouts_and_rendering.html#using-render 2.2.9 Rendering JSON says that you can render JSON in a view as follows: http://guides.rubyonrails.org/layouts_and_rendering.html#using-render 2.2.9渲染JSON表示您可以在视图中渲染JSON,如下所示:
render json: @product
I can't get this to work. 我无法使它正常工作。 In index.html.erb I have:
在index.html.erb中,我有:
render json: @trees
(@trees is defined as Tree.all which contains an Ancestry tree, FWIW - not relevant to the problem as far as I can tell.) (@trees定义为Tree.all,其中包含一棵祖先树FWIW-就我所知,与该问题无关。)
I get the following error on that line: 我在该行上收到以下错误:
You invoked render but did not give any of :partial, :template, :inline, :file, :plain, :text or :body option.
您调用了render,但未提供:partial,:template,:inline,:file,:plain,:text或:body选项中的任何一个。
I get the same error if I change that line of code to: 如果将那行代码更改为:我会收到相同的错误:
render json: {text:"hello world"}
What am I doing wrongly? 我做错了什么?
The render
call in this form should go into the controller. 这种形式的
render
调用应该进入控制器。 If you really want to render JSON in the view, <%= @trees.to_json %>
should do the trick. 如果您确实想在视图中呈现JSON,
<%= @trees.to_json %>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.