Skip to content

Instantly share code, notes, and snippets.

@andrebautista
Created October 25, 2013 03:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrebautista/7148857 to your computer and use it in GitHub Desktop.
Save andrebautista/7148857 to your computer and use it in GitHub Desktop.
class PostsController < ApplicationController
before_filter :authenticate_user!, except: [:index, :show]
# GET /posts
# GET /posts.json
def index
@posts = policy_scope(Post)
respond_to do |format|
format.html # index.html.erb
format.json { render json: @posts }
end
end
# GET /posts/1
# GET /posts/1.json
def show
@commentable = @post
@comments = @commentable.comments
@comment = Comment.new
respond_to do |format|
format.html # show.html.erb
format.json { render json: @post }
end
end
# GET /posts/new
# GET /posts/new.json
def new
@post = Post.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @post }
end
end
# GET /posts/1/edit
def edit
authorize @post
end
# POST /posts
# POST /posts.json
def create
@post = Post.new(post_params)
authorize @post
respond_to do |format|
if @post.save
current_user.posts << @post
format.html { redirect_to @post, notice: 'Post was successfully created.' }
format.json { render json: @post, status: :created, location: @post }
else
format.html { render action: "new" }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
# PUT /posts/1
# PUT /posts/1.json
def update
authorize @post
@post = Post.find(params[:id])
respond_to do |format|
if @post.update_attributes(params[:post])
format.html { redirect_to @post, notice: 'Post was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
# DELETE /posts/1
# DELETE /posts/1.json
def destroy
@post = Post.find(params[:id])
@post.destroy
respond_to do |format|
format.html { redirect_to posts_url notice: "Post was deleted." }
format.json { head :no_content }
end
end
end
class ProjectsController < ApplicationController
before_filter :authenticate_user!, except: [:index, :show]
def index
@projects = Project.all
end
def new
@project = Project.new
end
def create
@project = Project.new(params[:project])
if @project.save
flash[:notice] = "Project was successfully created"
redirect_to @project
else
render :new
end
end
def show
@project = Project.find(params[:id])
end
def edit
@project = Project.find(params[:id])
end
def update
@project = Project.find(params[:id])
if @project.update_attributes(params[:project])
redirect_to @project, notice: 'Project was successfully updated.'
else
render :edit
end
end
def destroy
@project = Project.find(params[:id])
@project.destroy
respond_to do |format|
format.html { redirect_to posts_url }
format.json { head :no_content }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment