Soc/app/controllers/blog_posts_controller.rb

68 lines
1.4 KiB
Ruby
Raw Permalink Normal View History

2023-10-05 18:46:09 +00:00
# frozen_string_literal: true
class BlogPostsController < ApplicationController
skip_before_action :require_login, only: [:index, :show]
before_action :set_blog_post, only: [:show, :edit, :update, :destroy]
def index
2023-10-13 19:28:35 +00:00
@blog_posts = BlogPost.published
.order(created_at: :desc)
.page(params[:page])
2023-10-05 18:46:09 +00:00
end
def new
@blog_post = BlogPost.new
end
def create
@blog_post = BlogPost.new(blog_post_params)
@blog_post.user = helpers.current_user
2023-10-12 15:24:33 +00:00
2023-10-05 18:46:09 +00:00
if @blog_post.save
redirect_to @blog_post, notice: t(".created")
else
render :new, status: :unprocessable_entity
end
end
def show
unless @blog_post.published || @blog_post.user == helpers.current_user
redirect_to blog_posts_path, notice: t(".not_found")
end
end
def edit; end
def update
if @blog_post.update(blog_post_params)
redirect_to @blog_post, notice: t(".updated")
else
render :edit, status: :unprocessable_entity
end
end
def destroy
if @blog_post.destroy
redirect_to blog_posts_path, notice: t(".deleted")
else
render :edit, status: :unprocessable_entity
end
end
private
def blog_post_params
params.require(:blog_post).permit(
:title,
:content,
2023-10-06 07:26:16 +00:00
:summary,
2023-10-05 18:46:09 +00:00
:published,
:slug,
)
end
def set_blog_post
@blog_post = BlogPost.find_by(slug: params[:id])
end
end