classCommentsController<ApplicationControllerrespond_to:jsondefindex@post=Post.find(params[:post_id])respond_with(@post,@post.comments)enddefshow@comment=Comment.find(params[:id])respond_with@commentend# for validation, can't use responders (batman expects errors to not have a root)defcreate@post=Post.find(params[:post_id])@comment=@post.comments.build(params[:comment])respond_todo|format|if@comment.saveformat.json{render:json=>@comment}elseformat.json{render:json=>@comment.errors,:status=>:unprocessable_entity}endendend# for validation, can't use responders (batman expects errors to not have a root)defupdate@comment=Comment.find(params[:id])respond_todo|format|if@comment.update_attributes(params[:comment])format.json{render:json=>@comment}elseformat.json{render:json=>@comment.errors,:status=>:unprocessable_entity}endendend# responder ok heredefdestroy@comment=Comment.find(params[:id])respond_with(@comment.post,@comment.destroy)endend
For this string of examples, we are using serializers (though I don’t use them anymore for reasons I might explain later):
%a.btn.btn-primary{'data-route'=>'routes.posts[post].comments.new'} New Comment
.row#content.span12#comments%div{"data-foreach-comment"=>"post.comments"}.comment{"data-partial"=>"comments/_comment"}
Here you can see how we refer to an association for routing purposes (not good form to click off to a separate page, but keeping it basic) as well as iterating over the association for view partials.
To the Rails developer, Batman should feel like idiomatic Rails written in Coffeescript, with similar project layout, MVC definition, and convenient macros for the things you use regularly. This association example demonstrates that it is exactly how you would expect it to work. Minimizing surprise is definitely a strength of Batman.