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 @@ -

Kurs: <%= @course.name %>

+

Kurs: <%= @course.name %> (<%= link_to raw(t('back')), admin_courses_path %>)

<%= @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 @@ -

Kursy

-

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? %> -
-

<%= pluralize(week.errors.count, "error") %> prohibited this admin_week from being saved:

- - -
+ <%= error_messages(week) %> <% end %> + <%= form.hidden_field :course_id %>
<%= form.label :name %> diff --git a/app/views/admin/weeks/partials/_index.html.erb b/app/views/admin/weeks/partials/_index.html.erb index 93840f6..6345983 100644 --- a/app/views/admin/weeks/partials/_index.html.erb +++ b/app/views/admin/weeks/partials/_index.html.erb @@ -4,7 +4,7 @@
Lista tygodni
- <%= link_to raw(' Dodaj Nowy Tydzien'), new_admin_week_path, { remote: true, class: 'btn btn-sm btn-primary float-right' } %> + <%= link_to raw(' Dodaj Nowy Tydzien'), new_admin_week_path(course_id: @course.id), { remote: true, class: 'btn btn-sm btn-primary float-right' } %>
diff --git a/config/locales/pl/pl.yml b/config/locales/pl/pl.yml index 1ad3904..d83707d 100644 --- a/config/locales/pl/pl.yml +++ b/config/locales/pl/pl.yml @@ -3,6 +3,7 @@ pl: logout: 'Wyloguj' login: 'Zaloguj' add: 'Dodaj' + back: 'Wróc' actions: 'Akcje' information: 'Informacja' empty_list: 'Nie znaleziono żadnych elementów do wyświetlenia.' diff --git a/config/locales/pl/weeks.yml b/config/locales/pl/weeks.yml index 7011682..26e7bdd 100644 --- a/config/locales/pl/weeks.yml +++ b/config/locales/pl/weeks.yml @@ -16,3 +16,4 @@ pl: week: name: 'Nazwa' description: 'Opis' + course: 'Kurs'