Reputation: 3386
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?
Upvotes: 1
Views: 899
Reputation: 14412
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.
Upvotes: 4