63 lines
1.2 KiB
Ruby
63 lines
1.2 KiB
Ruby
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)
|
|
# @post = Post.find_by(params[:id])
|
|
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
|