Skip to content

Instantly share code, notes, and snippets.

@rummelonp
Last active January 20, 2016 06:35
Show Gist options
  • Save rummelonp/6548805 to your computer and use it in GitHub Desktop.
Save rummelonp/6548805 to your computer and use it in GitHub Desktop.
標準的な Rails のアレ
# -*- 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