Skip to content

Instantly share code, notes, and snippets.

@vinniefranco
Created September 22, 2010 06:19
Show Gist options
  • Save vinniefranco/591228 to your computer and use it in GitHub Desktop.
Save vinniefranco/591228 to your computer and use it in GitHub Desktop.
class Admin::PostsController < ApplicationController
before_filter :authenticate_user!
respond_to :json
def index
@posts = Post.asc(:created_at)
respond_with(@posts)
end
def show
@post = Post.first(:conditions => { :id => params[:id] } )
end
def edit
@post = Post.first( :conditions => { :id => params[:id] } )
@form = render_to_string :partial => 'form'
respond_with(@post)
end
def new
@post = Post.new
@form = render_to_string :partial => 'form'
end
def create
@post = Post.new(params[:post])
respond_with(@post) do |format|
if @post.save
format.json {
render :json => { :flash => 'Post created', :id => @post.id },
:status => :created, :location => @post
}
else
format.json {
render :json => { :flash => 'Could not create Post', :errors => @post.errors },
:status => :unprocessable_entity
}
end
end
end
def update
@post = Post.first(:conditions => { :id => params[:id] } )
respond_with(@post) do |format|
if @post.update_attributes(params[:post])
format.json {
render :json => { :flash => 'Post updated', :id => @post.id },
:status => :created, :location => @post
}
else
format.json {
render :json => { :flash => 'Could not update Post', :errors => @post.errors },
:status => :unprocessable_entity
}
end
end
end
def destroy
Post.first(:conditions => { :id => params[:id] } ).destroy
redirect_to admin_posts_path
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment