Rails 3 basic Get JSON with jQuery and ask about the page flow

Rails newbie, thanks for the help in this, I hope, a basic question like JSON ....

I have a model of books in my application that belong to users.

What I would like to know how to do is to use jQuery to get a JSON object from books owned by the user. And then use jQuery to output the results to a div (id = targetdiv).

Where am I not sure, is this something that Rails 3 does out of the box? Because I see the .js answer, but I think JSON is what jQuery needs, right?

Thanks for helping me find out!

+4
source share
1 answer

From a Rails point of view, you can do something like this in the appropriate controller:

format.js { render :json => @user.books } 

Rails knows how to automatically serialize objects in JSON.

From a jQuery perspective, there are probably quite a few methods that you could use to convert the returned JSON to HTML and add it to the page. Often recommended http://github.com/jquery/jquery-tmpl

+1
source

All Articles