Add support for draft blog posts

This commit is contained in:
Trevor Vallender 2023-10-12 16:24:33 +01:00
parent 39e74891eb
commit 61140133f2
4 changed files with 11 additions and 2 deletions

View File

@ -5,7 +5,7 @@ class BlogPostsController < ApplicationController
before_action :set_blog_post, only: [:show, :edit, :update, :destroy]
def index
@blog_posts = BlogPost.all.order(created_at: :desc)
@blog_posts = BlogPost.published.order(created_at: :desc)
end
def new
@ -15,6 +15,7 @@ class BlogPostsController < ApplicationController
def create
@blog_post = BlogPost.new(blog_post_params)
@blog_post.user = helpers.current_user
if @blog_post.save
redirect_to @blog_post, notice: t(".created")
else

View File

@ -6,14 +6,18 @@ class BlogPost < ApplicationRecord
validates :title,
:user,
:published,
:slug,
:content,
:summary,
presence: true
validates :published, inclusion: { in: [true, false] }
validates :slug, uniqueness: true
scope :published, -> { where(published: true) }
scope :draft, -> { where(published: false) }
def to_param
slug
end

View File

@ -5,6 +5,7 @@ require "securerandom"
class User < ApplicationRecord
has_secure_password
has_many :microposts
has_many :blog_posts
validates :username,
:first_name,

View File

@ -4,4 +4,7 @@
<% if @user == current_user %>
<%= link_to t(".edit_user_details"), edit_user_path(@user) %>
<h2>Draft blog posts</h2>
<%= render @user.blog_posts.draft %>
<% end %>