Last active
January 20, 2016 06:35
-
-
Save rummelonp/6548805 to your computer and use it in GitHub Desktop.
標準的な Rails のアレ
This file contains hidden or 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
# -*- coding: utf-8 -*- | |
class PostController < ApplicationController | |
before_filter :require_post, only: [:show, :edit, :update, :destroy] | |
def index | |
@posts = posts | |
end | |
def new | |
@post = posts.new | |
end | |
def create | |
@post = posts.create(post_params) | |
if @post.save | |
redirect_to url_for(action: :show, id: @post.id), notice: '作成しました。' | |
else | |
render :new | |
end | |
end | |
def edit | |
end | |
def update | |
if @post.update_attributes(post_params) | |
redirect_to url_for(action: :show, id: @post.id), notice: '更新しました。' | |
else | |
render :edit | |
end | |
end | |
private | |
def posts | |
Post.where(user_id: current_user_id) | |
end | |
def require_post | |
@post = posts.find(params[:id]) | |
end | |
def post_params | |
params.require(:post).permit(:title, :content) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment