If not, how can I create this so that it is JSON-compatible with my AJAX requests?
Edit: It seems like I'm not clear about what I want to achieve. For example, let me assume that I have a post class that has properties such as ID, author, tag and content. The following is now ActiveModel :: Serializer which I will use the JSON response code:
class PostSerializer & lt; Now in the controller, I have to do something like Active MODEL :: SerialIiser features: ID, author, tag :: content, end
render: Jason = & Gt; To create a JSON file, @ post
looks like this
{"posts": [{"id": 1, "author": "John du", "tag": "test", "content": "hello world"}}
Now my purpose is to use JSONP so that when one requests Jsoncallback parameter (such as gET '/ posts? Jsoncallback = callback_function'), I would be able to generate the following:
callback_function ({"posts": [{"id": 1, "the author ":" John Doe "," tag ":" test "," content ":" hello world "}]})
The object and the result I am using to_json on the post is stored in a variable named @ posts_json, then specifies the content type as text / javascript in the controller, and renders the html.erb file Looks like:
& lt;% If the consultation [: jsoncallback] Block? & Gt%; & Lt;% = @ posts_json.html_safe% & gt; & Lt;% else% & gt; & Lt;% = params [: jsoncallback]% & gt; (<% = @ Posts_json.html_safe%>) & lt;% end% & gt;
I'm sure you would agree that this solution is really ugly, and the problem is that I'm starting to give problems due to the to_json method I'm using in the controller I am
In this way, I would like to use ActiveModel :: Serializer to get the same functionality: How can I do this?
So plainly in plain rail, render
syntax already callsback Handles. Just pass in the callback: Paramos [: jsoncallback] does the trick as an option.
Browse and browse under the Rendering JSON section for the document.
Comments
Post a Comment