Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Treebook show all users by json

On the Treebook aplication I want to be able to view all users and in json format too. How can I do this?

Please help! Thank-you!

1 Answer

Brandon Barrette
Brandon Barrette
20,485 Points

So in your controller, you would want to render json instead of html. For example:

def list_users
    @users = User.all

    respond to |format|
        format.html #this will render the list_users.html.erb view
        format.json { render json: @users }    #this will render your users in json
    end
end

If you only want it to return json, then comment out the format.html line. You might want to research jbuilder which could help you render the appropriate columns from your database (may not need to send everything).

Really good railscast about it: http://railscasts.com/episodes/320-jbuilder