简体   繁体   中英

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:

render json: @product

I can't get this to work. In index.html.erb I have:

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.)

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.

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. If you really want to render JSON in the view, <%= @trees.to_json %> should do the trick.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM