diff --git a/config/locales/en.yml b/config/locales/en.yml index 64308da..bec98ae 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -99,4 +99,6 @@ en: notice_invoice_not_found: "Invoice not found" notice_forbidden: "You do not have permission to access this resource" notice_issue_not_found: "Issue not found" - customer_details: "Customer Details" \ No newline at end of file + customer_details: "Customer Details" + notice_error_project_nil: "The issue's project is nil." + notice_error_project_nil: "The issue's tracker is nil." \ No newline at end of file diff --git a/lib/redmine_qbo/patches/issues_controller_patch.rb b/lib/redmine_qbo/patches/issues_controller_patch.rb index 69a9358..566e98c 100644 --- a/lib/redmine_qbo/patches/issues_controller_patch.rb +++ b/lib/redmine_qbo/patches/issues_controller_patch.rb @@ -22,11 +22,36 @@ module RedmineQbo end def self.included(base) - base.class_eval do helper Helper + before_action :error_check, only: [:create] + before_action :reload_new_issue, only: [:new] + end + end + + # Check for errors when creating an issue. + # If the project or tracker is not set, reload the new issue form with an error message. + def error_check + logger.info "Creating issue for: #{@issue.project}" + update_issue_from_params + if @issue.project.nil? + flash[:error] = t :notice_error_project_nil + render :new, status: :unprocessable_entity end + if @issue.project.nil? + flash[:error] = t :notice_error_tracker_nil + render :new, status: :unprocessable_entity + end + end + + # Reload the new issue form with a default tracker and project if not set. + # This is needed to prevent errors when creating an issue without selecting a project or tracker. + def reload_new_issue + logger.info "Reloading new #{@issue.tracker} issue for: #{@project}" + @issue.tracker ||= Tracker.first + @project ||= Project.first + logger.info "Reloaded new #{@issue.tracker} issue for: #{@project}" end end