From 95e7c215df89608a5b10bebb87968c9a713bbf3d Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Tue, 17 Oct 2023 15:05:36 -0400 Subject: Added uploading images to blog posts --- app/assets/javascripts/admin/records.coffee | 2 ++ app/assets/stylesheets/admin/layout.scss | 25 +++++++++++++++++++------ app/controllers/admin/blogs_controller.rb | 2 +- app/models/blog.rb | 7 +++++-- app/views/admin/blogs/_form.html.haml | 8 ++++++++ 5 files changed, 35 insertions(+), 9 deletions(-) (limited to 'app') diff --git a/app/assets/javascripts/admin/records.coffee b/app/assets/javascripts/admin/records.coffee index 27cdb78..3862b09 100644 --- a/app/assets/javascripts/admin/records.coffee +++ b/app/assets/javascripts/admin/records.coffee @@ -28,3 +28,5 @@ $(document).on "turbolinks:load", -> $(".tags-input input[type=text]").autocomplete({ source: Routes.suggest_tags_path() }) + $(".image-uploads pre").click -> + navigator.clipboard.writeText($(this).text()); diff --git a/app/assets/stylesheets/admin/layout.scss b/app/assets/stylesheets/admin/layout.scss index bf15b0d..9585414 100644 --- a/app/assets/stylesheets/admin/layout.scss +++ b/app/assets/stylesheets/admin/layout.scss @@ -82,19 +82,16 @@ body { } #entry-form { - display: flex; + display: grid; + grid-template-columns: 73% 27%; height: 100%; fieldset { border: 0; } - #content { - width: 77%; - } - #details { - width: 23%; + min-width: 0; display: flex; flex-direction: column; padding-left: 0; @@ -201,6 +198,22 @@ body { border-radius: 5px; padding: .5em; margin-bottom: 1em; + + h4 { + margin-top: 0; + margin-bottom: 0.5em; + } + + img { + max-width: 100%; + } +} + +.image-uploads { + pre { + overflow: auto; + max-width: 100%; + } } .should-create-record-field { diff --git a/app/controllers/admin/blogs_controller.rb b/app/controllers/admin/blogs_controller.rb index f0ce519..35f3514 100644 --- a/app/controllers/admin/blogs_controller.rb +++ b/app/controllers/admin/blogs_controller.rb @@ -57,7 +57,7 @@ class Admin::BlogsController < Admin::AdminController private def blog_params - params.require(:blog).permit(:title, :body, :slug, :published, :published_at, :tag_list, records_attributes: [:description, :_destroy]) + params.require(:blog).permit(:title, :body, :slug, :published, :published_at, :tag_list, images: [], records_attributes: [:description, :_destroy]) end def set_section diff --git a/app/models/blog.rb b/app/models/blog.rb index 362f69f..6db75ec 100644 --- a/app/models/blog.rb +++ b/app/models/blog.rb @@ -6,12 +6,15 @@ class Blog < ApplicationRecord has_many :comments belongs_to :user + has_many_attached :images do |attachable| + attachable.variant :thumb, resize_to_limit: [300, 300] + end + validates :title, presence: true validates :body, presence: true, if: :published validates :slug, presence: true, format: /\A[-a-z0-9]+\z/, if: :published validates :user, presence: true - - has_many_attached :images + validates :images, content_type: ['image/png', 'image/jpeg'] before_validation :set_draft_title before_save :set_published_at diff --git a/app/views/admin/blogs/_form.html.haml b/app/views/admin/blogs/_form.html.haml index 625c85e..c3720ba 100644 --- a/app/views/admin/blogs/_form.html.haml +++ b/app/views/admin/blogs/_form.html.haml @@ -39,4 +39,12 @@ = builder.label :_destroy, "Create record?" .record-description-field = builder.text_area :description, placeholder: "record text" + .details-module.image-uploads + %h4 Images + - f.object.images.each do |image| + = image_tag image.variant(:thumb) + = f.hidden_field :images, multiple: true, value: image.signed_id + %pre= url_for image + %hr + = f.file_field :images, multiple: true .details-module= f.submit -- cgit 1.4.1