rails8-memoapp/app/controllers/memos_controller.rb

72 lines
1.7 KiB
Ruby
Raw Normal View History

2025-02-20 22:46:52 +09:00
class MemosController < ApplicationController
before_action :set_memo, only: %i[edit update destroy]
before_action :authenticate_user!
2025-02-20 22:46:52 +09:00
def index
@q = current_user.memos.ransack(params[:q])
@memos = @q.result(distinct: true)
.order(created_at: :desc)
.page(params[:page])
.per(4)
2025-02-20 22:46:52 +09:00
respond_to do |format|
format.html
format.turbo_stream
end
end
def new
@memo = current_user.memos.build
2025-02-20 22:46:52 +09:00
end
def edit
@memo = current_user.memos.find(params[:id])
end
2025-02-20 22:46:52 +09:00
def create
@memo = current_user.memos.build(memo_params)
2025-02-20 22:46:52 +09:00
respond_to do |format|
if @memo.save
format.html { redirect_to memos_path, status: :see_other }
format.turbo_stream
else
format.html { render :new, status: :unprocessable_entity }
format.turbo_stream { render :new, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @memo.update(memo_params)
format.html { redirect_to memos_path, status: :see_other }
2025-02-24 22:51:13 +09:00
format.turbo_stream { redirect_to memos_path, status: :see_other }
2025-02-20 22:46:52 +09:00
else
format.html { render :edit, status: :unprocessable_entity }
format.turbo_stream { render :edit, status: :unprocessable_entity }
end
end
end
def destroy
@memo.destroy
respond_to do |format|
format.html { redirect_to memos_path, status: :see_other }
format.turbo_stream
end
end
private
def set_memo
@memo = current_user.memos.find_by(id: params[:id])
return unless @memo.nil?
redirect_to memos_path, alert: 'メモが見つかりませんでした'
2025-02-20 22:46:52 +09:00
end
def memo_params
params.require(:memo).permit(:content, :image)
2025-02-20 22:46:52 +09:00
end
end