Basic blog post CRUD

This commit is contained in:
Trevor Vallender 2023-10-05 19:46:09 +01:00
parent 592120aeae
commit 1e6a31ae78
13 changed files with 181 additions and 0 deletions

View File

@ -0,0 +1,40 @@
form#blog_post_form {
display: flex;
flex-direction: column;
max-width: 55em;
> h2 {
width: 100%;
}
> trix-toolbar {
max-width: 95%;
}
> trix-editor {
width: 95%;
> ul {
list-style-type: disc;
}
}
input[type=text] {
width: 95%;
}
> input[type=submit] {
width: 95%;
}
}
.blog_post {
margin: 1em auto;
padding: .5em;
background-color: var(--inset-background-color);
border: 1px solid var(--border-color);
> .created_at {
text-align: right;
font-size: .8em;
}
}

View File

@ -0,0 +1,63 @@
# 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
@blog_posts = BlogPost.all.order(created_at: :desc)
end
def new
@blog_post = BlogPost.new
end
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
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,
:published,
:slug,
)
end
def set_blog_post
@blog_post = BlogPost.find_by(slug: params[:id])
end
end

View File

@ -0,0 +1,8 @@
<div id="blog_post_<%= blog_post.id %>" class="blog_post">
<h2><%= blog_post.title %></h2>
<%= blog_post.content %>
<div class="created_at">
<%= link_to "Edit", edit_blog_post_path(blog_post) if blog_post.user == current_user %>
<%= link_to blog_post.created_at.strftime("%Y-%m-%d %H:%M"), blog_post %>
</div>
</div>

View File

@ -0,0 +1,15 @@
<%= form_with model: @blog_post, id: "blog_post_form" do |f| %>
<h2><%= title %></h2>
<%= f.text_field :title, placeholder: t(".title") %>
<%= f.rich_text_area :content %>
<%= f.label :slug %>
<%= f.text_field :slug %>
<%= f.label :published %>
<%= f.check_box :published %>
<%= f.submit button_text %>
<% end %>

View File

@ -0,0 +1,10 @@
<% @title = t(".edit") %>
<%= render partial: "form",
locals: {
user: @user,
button_text: t(".edit"),
title: t(".edit"),
} %>
<%= link_to t(".delete"), @blog_post, data: { turbo_method: :delete, turbo_confirm: t(".confirm") } %>

View File

@ -0,0 +1,13 @@
<h2><%= t(".blog_posts") %></h2>
<%= link_to t(".new"), new_blog_post_path if logged_in? %>
<% if @blog_posts.empty? %>
<p><%= t(".empty") %></p>
<% else %>
<ul id="blog_posts">
<% @blog_posts.each do |blog_post| %>
<li><%= render blog_post %></li>
<% end %>
</ul>
<% end %>

View File

@ -0,0 +1,9 @@
<% @title = t(".create") %>
<%= render partial: "form",
locals: {
user: @user,
button_text: t(".create"),
title: t(".create"),
} %>

View File

@ -0,0 +1 @@
<%= render @blog_post %>

View File

@ -18,6 +18,7 @@
<nav> <nav>
<ul> <ul>
<li><%= link_to t(".home"), root_path %></li> <li><%= link_to t(".home"), root_path %></li>
<li><%= link_to t(".blog_posts"), blog_posts_path %></li>
<li><%= link_to t(".microposts"), microposts_path %></li> <li><%= link_to t(".microposts"), microposts_path %></li>
<% if logged_in? %> <% if logged_in? %>
<li><%= link_to t(".profile"), user_path(current_user) %></li> <li><%= link_to t(".profile"), user_path(current_user) %></li>

View File

@ -0,0 +1,5 @@
en:
blog_posts:
created: Successfully created blog post
updated: Successfully updated blog post
deleted: Successfully deleted blog post

View File

@ -0,0 +1,14 @@
en:
blog_posts:
index:
blog_posts: blog posts
empty: You have no blog posts yet.
new: New blog post
new:
create: Create blog post
edit:
edit: Edit blog post
delete: Delete blog post
confirm: Are you sure you want to delete this blog post?
form:
title: Title

View File

@ -7,4 +7,5 @@ en:
log_out: Log out log_out: Log out
profile: Profile profile: Profile
register: Register register: Register
blog_posts: Blog
microposts: μposts microposts: μposts

View File

@ -12,5 +12,6 @@ Rails.application.routes.draw do
delete "log_out", to: "sessions#destroy_session" delete "log_out", to: "sessions#destroy_session"
get "confirm_email", to: "email_confirmations#confirm" get "confirm_email", to: "email_confirmations#confirm"
resources :blog_posts
resources :microposts resources :microposts
end end