Skip to content

Instantly share code, notes, and snippets.

@excid3
Created March 31, 2016 15:26
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 excid3/6350303f58347eb5d1f11f8a6029e8eb to your computer and use it in GitHub Desktop.
Save excid3/6350303f58347eb5d1f11f8a6029e8eb to your computer and use it in GitHub Desktop.
class PostsController < ApplicationController
before_action :set_posts, only: [:index]
before_action :set_new_post, only: [:new, :create]
before_action :set_post, only: [:show, :edit, :update, :destroy]
def new; end
def show; end
def edit; end
def index; end
def create
@post.attributes = post_params
if @post.save
redirect_to @post
else
render :new
end
end
def update
if @post.update_attributes(post_params)
redirect_to @post
else
render :edit
end
end
def destroy
@post.destroy!
redirect_to :index
end
private
def set_new_post
@post = Post.new
end
def set_post
@post = posts.find(params[:id])
end
def set_posts
@posts = current_user.posts
end
def post_params
@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