Skip to content

Instantly share code, notes, and snippets.

@eladmeidar
Created September 1, 2015 12:23
Show Gist options
  • Save eladmeidar/38241da5cfa58bfd5d1b to your computer and use it in GitHub Desktop.
Save eladmeidar/38241da5cfa58bfd5d1b to your computer and use it in GitHub Desktop.
perfect controller
class PostsController < ApplicationController
before_action :find_post, only: [:show, :edit, :destroy]
skip_before_action :verify_authenticity_token, only: [:create, :update, :destroy]
respond_to :html, :json, :xml
def index
@posts = Post.all
respond_with(@posts)
end
def show
respond_with(@post)
end
def new
@post = Post.new
respond_with(@post)
end
def create
@post = Post.new(post_params)
@post.save
respond_with(@post)
end
def edit
end
def update
@post = Post.find(params[:id])
if @post.update_attributes(post_params)
else
end
end
def destroy
if @post.destroy
else
end
end
protected
def find_post
@post = Post.find(params[:id])
end
def post_params
params.require(:post).permit(:title, :body)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment