diff --git a/app/assets/stylesheets/blog_posts.css b/app/assets/stylesheets/blog_posts.css new file mode 100644 index 0000000..7fe1cda --- /dev/null +++ b/app/assets/stylesheets/blog_posts.css @@ -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; + } +} diff --git a/app/controllers/blog_posts_controller.rb b/app/controllers/blog_posts_controller.rb new file mode 100644 index 0000000..eab1540 --- /dev/null +++ b/app/controllers/blog_posts_controller.rb @@ -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 diff --git a/app/views/blog_posts/_blog_post.html.erb b/app/views/blog_posts/_blog_post.html.erb new file mode 100644 index 0000000..c5ad15c --- /dev/null +++ b/app/views/blog_posts/_blog_post.html.erb @@ -0,0 +1,8 @@ +
<%= t(".empty") %>
+<% else %> +