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

View File

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

View File

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

View File

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