Created
March 31, 2016 15:26
-
-
Save excid3/6350303f58347eb5d1f11f8a6029e8eb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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