diff --git a/app/controllers/schemes_controller.rb b/app/controllers/schemes_controller.rb index 1ef3ae211..7b2ae134d 100644 --- a/app/controllers/schemes_controller.rb +++ b/app/controllers/schemes_controller.rb @@ -30,8 +30,12 @@ class SchemesController < ApplicationController end def create - @scheme = Scheme.create!(scheme_params) - render "schemes/primary_client_group" + @scheme = Scheme.new(scheme_params) + if @scheme.save + render "schemes/primary_client_group" + else + render :new, status: :unprocessable_entity + end end def update diff --git a/app/models/scheme.rb b/app/models/scheme.rb index e2036c7ed..ea2f883a7 100644 --- a/app/models/scheme.rb +++ b/app/models/scheme.rb @@ -5,6 +5,8 @@ class Scheme < ApplicationRecord has_many :locations has_many :case_logs + validates :organisation_id, presence: { message: I18n.t("validations.scheme.id_missing") } + scope :search_by_code, ->(code) { where("code ILIKE ?", "%#{code}%") } scope :search_by_service_name, ->(name) { where("service_name ILIKE ?", "%#{name}%") } scope :search_by_postcode, ->(postcode) { joins(:locations).where("locations.postcode ILIKE ?", "%#{postcode.delete(' ')}%") } diff --git a/config/locales/en.yml b/config/locales/en.yml index f073b658a..420902aec 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -39,6 +39,9 @@ en: reset_password: "Reset your password" validations: + scheme: + id_missing: "Enter the organisation’s name" + organisation: name_missing: "Enter the organisation’s name" provider_type_missing: "Select the organisation’s type"