2023-09-17 13:59:06 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class MicropostsController < ApplicationController
|
2023-09-29 13:29:50 +00:00
|
|
|
skip_before_action :require_login, only: [:index, :show]
|
2023-10-05 18:05:53 +00:00
|
|
|
before_action :set_micropost, only: [:show, :edit, :update, :destroy]
|
2023-09-17 13:59:06 +00:00
|
|
|
|
|
|
|
def index
|
2023-10-13 19:28:35 +00:00
|
|
|
@microposts = Micropost.all
|
|
|
|
.order(created_at: :desc)
|
|
|
|
.page(params[:page])
|
2023-09-17 13:59:06 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def new
|
|
|
|
@micropost = Micropost.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
micropost = Micropost.new(micropost_params)
|
|
|
|
micropost.user = helpers.current_user
|
|
|
|
if micropost.save
|
|
|
|
redirect_to micropost, notice: t(".created")
|
|
|
|
else
|
|
|
|
render :new, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def show; end
|
|
|
|
|
2023-10-05 18:05:53 +00:00
|
|
|
def edit; end
|
|
|
|
|
|
|
|
def update
|
|
|
|
if @micropost.update(micropost_params)
|
|
|
|
redirect_to @micropost, notice: t(".updated")
|
|
|
|
else
|
|
|
|
render :edit, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
if @micropost.destroy
|
|
|
|
redirect_to microposts_path, notice: t(".deleted")
|
|
|
|
else
|
|
|
|
render :edit, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-09-17 13:59:06 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def micropost_params
|
|
|
|
params.require(:micropost).permit(
|
|
|
|
:content,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_micropost
|
|
|
|
@micropost = Micropost.find(params[:id])
|
|
|
|
end
|
|
|
|
end
|