diff --git a/app/controllers/admin/profile_fields_controller.rb b/app/controllers/admin/profile_fields_controller.rb index 218d9e6f4..977079348 100644 --- a/app/controllers/admin/profile_fields_controller.rb +++ b/app/controllers/admin/profile_fields_controller.rb @@ -1,5 +1,8 @@ module Admin class ProfileFieldsController < Admin::ApplicationController + ALLOWED_PARAMS = %i[ + input_type label active placeholder_text description + ].freeze layout "admin" def index @@ -7,26 +10,41 @@ module Admin end def update - @profile_fields = ProfileField.find(params[:id]) - @profile_fields.update!(profile_field_params) + profile_field = ProfileField.find(params[:id]) + if profile_field.update(profile_field_params) + flash[:success] = "Profile field #{profile_field.label} updated" + else + flash[:error] = "Error: #{profile_field.errors_as_sentence}" + end redirect_to admin_profile_fields_path end def create - @profile_field = ProfileField.create(profile_field_params) + profile_field = ProfileField.new(profile_field_params) + if profile_field.save + flash[:success] = "Profile field #{profile_field.label} created" + else + flash[:error] = "Error: #{profile_field.errors_as_sentence}" + end redirect_to admin_profile_fields_path end def destroy - @profile_field = ProfileField.find(params[:id]) - @profile_field.destroy + profile_field = ProfileField.find(params[:id]) + if profile_field.destroy + flash[:success] = "Profile field #{profile_field.label} deleted" + else + flash[:error] = "Error: #{profile_field.errors_as_sentence}" + end redirect_to admin_profile_fields_path end private + private_constant :ALLOWED_PARAMS + def profile_field_params - allowed_params = %i[input_type label active placeholder_text description] + allowed_params = ALLOWED_PARAMS params.require(:profile_field).permit(allowed_params) end end diff --git a/app/models/profile_field.rb b/app/models/profile_field.rb index c09e9e8fc..d53fe9e6f 100644 --- a/app/models/profile_field.rb +++ b/app/models/profile_field.rb @@ -1,13 +1,11 @@ class ProfileField < ApplicationRecord # Key names follow the Rails form helpers - INPUT_TYPES = { + enum input_type: { text_field: 0, text_area: 1, check_box: 2, color_field: 3 - }.freeze - - enum input_type: INPUT_TYPES + } validates :label, presence: true, uniqueness: { case_sensitive: false } validates :active, inclusion: { in: [true, false] } diff --git a/app/views/admin/profile_fields/_form.html.erb b/app/views/admin/profile_fields/_form.html.erb index b7b46d041..4e6577bdb 100644 --- a/app/views/admin/profile_fields/_form.html.erb +++ b/app/views/admin/profile_fields/_form.html.erb @@ -12,7 +12,7 @@