diff --git a/app/controllers/admin/weeks_controller.rb b/app/controllers/admin/weeks_controller.rb index 938b0e9..59eda1a 100644 --- a/app/controllers/admin/weeks_controller.rb +++ b/app/controllers/admin/weeks_controller.rb @@ -27,7 +27,8 @@ module Admin # POST /admin/weeks.json def create @week = Week.new(admin_week_params) - + @course = Course.find(@week.course_id) + params[:course_id] = @course.id respond_to do |format| if @week.save format.js { collection } @@ -81,7 +82,7 @@ module Admin # Never trust parameters from the scary internet, only allow the white list through. def admin_week_params - params.require(:week).permit(:name, :description) + params.require(:week).permit(:name, :description, :course_id) end end end diff --git a/app/helpers/errors_helper.rb b/app/helpers/errors_helper.rb new file mode 100644 index 0000000..4c846f7 --- /dev/null +++ b/app/helpers/errors_helper.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +# ErrorsHelper +module ErrorsHelper + def error_messages(object) + return unless object.errors.any? + + content_tag(:div, class: 'card bg-danger p-3 text-white') do + concat error_messages_h4 + concat error_messages_ul(object) + end + end + + private + + def error_messages_h4 + content_tag(:h4) do + concat content_tag(:i, '', class: 'fas fa-exclamation-circle') + concat " #{I18n.t('errors_ocurred')}" + end + end + + def error_messages_ul(object) + content_tag(:ul, class: 'm-0') do + object.errors.full_messages.each do |message| + concat content_tag(:li, message) + end + end + end +end diff --git a/app/models/week.rb b/app/models/week.rb index 25ecc48..34df9ec 100644 --- a/app/models/week.rb +++ b/app/models/week.rb @@ -1,8 +1,11 @@ # frozen_string_literal: true +# Weeks class Week < ApplicationRecord belongs_to :course + validates :name, presence: true + scope :by_course, ->(c_id) { where(course_id: c_id) } scope :name_asc, -> { order('name ASC') } scope :name_desc, -> { order('name DESC') } diff --git a/app/views/admin/courses/show.html.erb b/app/views/admin/courses/show.html.erb index 5a4b713..99f6705 100644 --- a/app/views/admin/courses/show.html.erb +++ b/app/views/admin/courses/show.html.erb @@ -1,6 +1,4 @@ -
<%= @course.description %>
<%= render partial: '/admin/weeks/partials/index' %> - -<%= link_to 'Back', admin_courses_path %> diff --git a/app/views/admin/weeks/index.html.erb b/app/views/admin/weeks/index.html.erb index cd79d40..e6b7c15 100644 --- a/app/views/admin/weeks/index.html.erb +++ b/app/views/admin/weeks/index.html.erb @@ -1,4 +1 @@ -Tutaj jakiś opis.
-<%= notice %>
<%= render partial: '/admin/weeks/partials/index' %> diff --git a/app/views/admin/weeks/partials/_form.html.erb b/app/views/admin/weeks/partials/_form.html.erb index 2e62bab..082bf68 100644 --- a/app/views/admin/weeks/partials/_form.html.erb +++ b/app/views/admin/weeks/partials/_form.html.erb @@ -1,15 +1,8 @@ <%= form_with(model: [:admin, week]) do |form| %> <% if week.errors.any? %> -