67 lines
1.5 KiB
Ruby
67 lines
1.5 KiB
Ruby
class MemosController < ApplicationController
|
|
before_action :set_memo, only: %i[edit update destroy]
|
|
|
|
def index
|
|
page = (params[:page] || 0).to_i
|
|
@memos = Memo.search(params[:query])
|
|
.order(created_at: :desc)
|
|
.limit(4)
|
|
.offset(page * 4)
|
|
|
|
@has_next = Memo.search(params[:query]).count > (page + 1) * 4
|
|
|
|
respond_to do |format|
|
|
format.html
|
|
format.turbo_stream
|
|
end
|
|
end
|
|
|
|
def new
|
|
@memo = Memo.new
|
|
end
|
|
|
|
def edit; end
|
|
|
|
def create
|
|
@memo = Memo.new(memo_params)
|
|
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 }
|
|
format.turbo_stream { render turbo_stream: turbo_stream.replace(@memo) }
|
|
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 = Memo.find(params[:id])
|
|
end
|
|
|
|
def memo_params
|
|
params.require(:memo).permit(:content)
|
|
end
|
|
end
|