From 1099a080de2d2f6c54fec4fde5470c83605e6bda Mon Sep 17 00:00:00 2001 From: BlazingRockStorm Date: Tue, 20 Aug 2024 00:53:35 +0900 Subject: [PATCH] rewrite form for album --- app/controllers/application_controller.rb | 2 +- app/views/admin/albums/_form.html.slim | 22 +++++++++++++++++ app/views/admin/albums/edit.html.slim | 29 ++++------------------- app/views/admin/albums/new.html.slim | 29 ++++------------------- 4 files changed, 31 insertions(+), 51 deletions(-) create mode 100644 app/views/admin/albums/_form.html.slim diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 8607d84..6e89e88 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -3,7 +3,7 @@ class ApplicationController < ActionController::Base # rubocop:disable Naming/VariableNumber rescue_from Exception, with: :render_500 if Rails.env.production? - rescue_from ActionController::RoutingError, with: :render_404 if Rails.env.production? + rescue_from ActionController::RoutingError, with: :render_404 if Rails.env.production? rescue_from ActiveRecord::RecordNotFound, with: :render_404 if Rails.env.production? # rubocop:enable Naming/VariableNumber diff --git a/app/views/admin/albums/_form.html.slim b/app/views/admin/albums/_form.html.slim new file mode 100644 index 0000000..f201c78 --- /dev/null +++ b/app/views/admin/albums/_form.html.slim @@ -0,0 +1,22 @@ += form_with model: [:admin, @album], local: true do |form| + - if @album.errors.any? + .alert.alert-danger + h2 = "#{pluralize(@album.errors.count, "error")} prohibited this album from being saved:" + ul + - @album.errors.each do |error| + li = error.full_message + + .mb-3 + = form.label :name, class: 'form-label' + = form.text_field :name, class: 'form-control' + + .mb-3 + = form.label :artist_id, class: 'form-label' + = form.select :artist_id, Artist.all.collect {|artist| [ artist.name, artist.id ] }, {}, class: 'form-control' + + .mb-3 + = form.label :release_year, class: 'form-label' + = form.number_field :release_year, class: 'form-control' + + div + = form.submit class: 'btn btn-primary' diff --git a/app/views/admin/albums/edit.html.slim b/app/views/admin/albums/edit.html.slim index 0c2502b..cf35cc5 100644 --- a/app/views/admin/albums/edit.html.slim +++ b/app/views/admin/albums/edit.html.slim @@ -1,38 +1,17 @@ -== render "layouts/navbar" +== render 'layouts/navbar' .container-fluid .row.lyrics-site-content - == render "layouts/sidebar" + == render 'layouts/sidebar' main.col-md-9.col-lg-10.mt-2.mb-2 h1 Editing album - = form_with scope: :album, url: admin_album_path(@album), method: :patch do |form| - - if @album.errors.any? - div.alert.alert-danger - h2 = "#{pluralize(@album.errors.count, "error")} prohibited this album from being saved:" - ul - - @album.errors.each do |error| - li = error.full_message - - .mb-3 - = form.label :name, class: 'form-label' - = form.text_field :name, class: 'form-control' - - .mb-3 - = form.label :artist_id, class: 'form-label' - = form.select :artist_id, Artist.all.collect {|artist| [ artist.name, artist.id ] }, {}, class: 'form-control' - - .mb-3 - = form.label :release_year, class: 'form-label' - = form.number_field :release_year, class: 'form-control' - - div - = form.submit 'Save Album', class: 'btn btn-primary' + == render 'form', album: @album br div - = link_to "Back to albums", admin_albums_path, class: 'btn btn-secondary' + = link_to 'Back to albums', admin_albums_path, class: 'btn btn-secondary' == render 'layouts/footer' diff --git a/app/views/admin/albums/new.html.slim b/app/views/admin/albums/new.html.slim index eb86501..e9775dc 100644 --- a/app/views/admin/albums/new.html.slim +++ b/app/views/admin/albums/new.html.slim @@ -1,38 +1,17 @@ -== render "layouts/navbar" +== render 'layouts/navbar' .container-fluid .row.lyrics-site-content - == render "layouts/sidebar" + == render 'layouts/sidebar' main.col-md-9.col-lg-10.mt-2.mb-2 h1 New album - = form_with scope: :album, url: admin_albums_path do |form| - - if @album.errors.any? - .alert.alert-danger - h2 = "#{pluralize(@album.errors.count, "error")} prohibited this album from being saved:" - ul - - @album.errors.each do |error| - li = error.full_message - - .mb-3 - = form.label :name, class: 'form-label' - = form.text_field :name, class: 'form-control' - - .mb-3 - = form.label :artist_id, class: 'form-label' - = form.select :artist_id, Artist.all.collect {|artist| [ artist.name, artist.id ] }, {}, class: 'form-control' - - .mb-3 - = form.label :release_year, class: 'form-label' - = form.number_field :release_year, class: 'form-control' - - div - = form.submit 'Create Album', class: 'btn btn-primary' + == render 'form', album: @album br div - = link_to "Back to albums", admin_albums_path, class: 'btn btn-secondary' + = link_to 'Back to albums', admin_albums_path, class: 'btn btn-secondary' == render 'layouts/footer'