LoginSignup
0
0

More than 3 years have passed since last update.

posts_controller.rb

Posted at

class PostsController < ApplicationController
def index
@posts = Post.all.order(created_at: :desc)
end

def show
@post = Post.find_by(id: params[:id])
end

def new
@post = Post.new
end

def create
@post = Post.new(content: params[:content])
if @post.save
# 変数flash[:notice]に、指定されたメッセージを代入してください
flash[:notice]="投稿を作成しました"
redirect_to("/posts/index")
else
render("posts/new")
end
end

def edit
@post = Post.find_by(id: params[:id])
end

def update
@post = Post.find_by(id: params[:id])
@post.content = params[:content]
if @post.save
flash[:notice] = "投稿を編集しました"
redirect_to("/posts/index")
else
render("posts/edit")
end
end

def destroy
@post = Post.find_by(id: params[:id])
@post.destroy
# 変数flash[:notice]に、指定されたメッセージを代入してください
flash[:notice] = "投稿を削除しました"
redirect_to("/posts/index")
end

end

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0