From 0376d7ccc96e75a06832187b39744bc5293f1c0b Mon Sep 17 00:00:00 2001 From: Ryan Scott Lewis Date: Tue, 10 Apr 2012 10:30:21 -0400 Subject: [PATCH] Added layout example to README --- README.md | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/README.md b/README.md index 8ea58ac..6c6bebc 100644 --- a/README.md +++ b/README.md @@ -168,6 +168,64 @@ You will get something like: } ``` +### Using layouts + +To use a layouts, create `app/views/application/application.json.json_builder`: + +```ruby +success @success +data JSON.parse(yield) if @success +``` + +Then, you can validate within your controllers: + +`app/controllers/posts_controller.rb`: + +```ruby +class PostsController < ApplicationController + before_filter :assume_success + respond_to :html, :json + + def show + @post = Post.where(id: 123).first + @success = false if @post.nil? + end + + private + + def assume_success + @success = true + end +end +``` + +`app/views/posts/show.json.json_builder`: + +```ruby +title @post.title +body @post.body +``` + +Now, when you navigate to `/posts/123.json` and it exists, you will get the follow JSON response: + +```json +{ + "success": true, + "data" : { + "title" : "Title of post 123", + "body" : "This is the body text of post 123" + } +} +``` + +And when post 123 does not exist: + +```json +{ + "success": false, +} +``` + ### Including JSONP callbacks Out of the box JSON Builder supports JSONP callbacks when used within a Rails project just by using the `callback` parameter. For instance, if you requested `/users.json?callback=myjscallback`, you'll get a callback wrapping the response: