0

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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.