playground/app/controllers/posts_controller.rb

64 lines
1.2 KiB
Ruby
Raw Permalink Normal View History

2026-05-17 03:44:36 +00:00
class PostsController < ApplicationController
before_action :authenticate_user!, only: [:new, :create, :edit, :update, :destroy]
allow_unauthenticated_access(only: [:index, :show])
def index
@posts = Post.all.order(created_at: :desc)
2026-05-17 17:41:08 +00:00
# @post = Post.find_by(params[:id])
2026-05-17 03:44:36 +00:00
end
def show
@post = Post.find(params[:id])
end
def new
@user = Current.user
@post = Post.new
end
def create
@post = Post.new(post_params)
if @post.save
redirect_to posts_path, notice: "post created."
else
render :new, status: :unprocessable_entity, notice: "U fucked up somewhere."
end
end
def edit
@post = Post.find(params[:id])
end
def update
@post = Post.find(params[:id])
if @post.update(post_params)
redirect_to posts_path, notice: "post edited."
else
render :edit, status: :unprocessable_entity, notice: "U fucked up somewhere."
end
end
def destroy
@post = Post.find(params[:id])
@post.destroy
redirect_to posts_path, notice: "Post deleted."
end
private
def post_params
params.require(:post).permit(:text, :icon_image)
end
def set_user
@user = Current.user
end
end