Skip to content

Instantly share code, notes, and snippets.

@amrnt
Created October 17, 2010 11:06
Show Gist options
  • Save amrnt/630755 to your computer and use it in GitHub Desktop.
Save amrnt/630755 to your computer and use it in GitHub Desktop.
(function($) {
var Post, app;
Post = Model("post", {
persistence: Model.REST("/posts")
});
app = $.sammy(function() {
this.element_selector = '#body';
this.get('#!/', function(context) {
var posts;
posts = Post.all();
return context.log(posts);
});
this.get('#!/posts/:id', function(context) {
var post;
post = Post.find(this.params.id);
return context.log(post);
});
this.post('#!/posts', function(context) {
var post;
post = new Post({
title: "stuff"
});
return post.save();
});
return true;
});
$(function() {
return app.run('#!/');
});
return true;
})(jQuery);
class PostsController < ApplicationController
def index
@posts = Post.all
render :json => @posts
end
def show
@post = Post.find(params[:id])
render :json => @post
end
def create
@post = Post.new(params[:post])
if @post.save
render :json => @post, :status => :created, :location => @post
else
render :json => @post.errors, :status => :unprocessable_entity
end
end
def update
@post = Post.find(params[:id])
if @post.update_attributes(params[:post])
head :ok
else
render :json => @post.errors, :status => :unprocessable_entity
end
end
def destroy
@post = Post.find(params[:id])
@post.destroy
head :ok
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment