views:

99

answers:

1

Rails newbie, thanks for help with this hopefully basic JSON type question....

I have a model Books in my app, which belong_to users.

What'd I'd like to learn how to do, is use jQuery to get a JSON object of the books that the user owns. And then use jQuery to list the results out on a div (id=targetdiv).

Where I'm not certain, is is this something Rails 3 does out of the box? Because I see the respond .js, but I think JSON is what is needed by jQuery, right?

Thanks for helping me learn this!

A: 

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

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

Rails knows how to serialize objects to JSON automatically.

From the jQuery point of view, there are probably quite a few methods you could use to convert the returned JSON to HTML and add it to the page. One that's often recommended is http://github.com/jquery/jquery-tmpl

malclocke