Reputation: 1796
I need to output custom JSON in order to be backwards compatible with existing software, which is written in Javascript, so I need to wrap my JSON with "parseDate(" at the beginning of it, and ");" at the end of it.
I tried doing it in controller like this
def index
@data = Data.all
@products = Product.all
respond_to do |format|
format.html
format.json {render :json => { :products => {:product => @data.name}}}
end
end
And then specify it in the view:
app/views/products.json.erb
<%= p "parseData(" %>
<%= render :json %>
<%= p "};" %>
But it outputs pure JSON completely skipping both "parseData(" and ");", why? How do I make JSON to be printed in the middle of the view and then append strings to the top and bottom?
Upvotes: 0
Views: 85
Reputation: 176402
JSON renderer supports a callback option.
format.json { render :json => { :products => {:product => @data.name }}, :callback => 'parseDate' }
You can read the implementation in the renderer.rb
source code.
Upvotes: 1