Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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