jeudi 2 juillet 2015

Ruby on Rails update ActiveRecord::Error string in the errors hash

I have an error message that says, Patient Contact Information: Value must be a 10 digit number

...but I want it to say Patient phone number must be a 10 digit number

I have an ActiveRecord Error being added here based on an attribute ...

<<-EOS

          # Validate as Phone Number
          unless self.#{ddv.attribute_name}.blank?
            self.update_attributes(#{ddv.attribute_name}: self.#{ddv.attribute_name}.gsub(/\\\D/, ''))
            if self.#{ddv.attribute_name}.length != 10
              errors.add(:#{ddv.attribute_name}, "must be a 10 digit number")
            end
          end
        EOS

while debugging I know that ddv.attribute_name = 'value' and ddv.model_name = "PatientContactInformation

I'm not sure how rails handles the next part, but ActiveRecord somehow adds the error message Patient Contact Information: Value must be a 10 digit number via the line errors.add(:#{ddv.attribute_name}, "must be a 10 digit

in other words, ddv.attribute_name is actually displaying ddv.model_name

the error message gets flashed on the screen in this action controller ...

  def submit_ucf
    @universal_claim_form = UniversalClaimForm.find(params[:id])
    @universal_claim_form.attributes = params[:universal_claim_form]
    begin
      @universal_claim_form.submitted_by = current_user.email
      if @universal_claim_form.save_then_submit(current_user.account_id)
        flash[:success] = 'Form was successfully submitted.'
        redirect_to universal_claim_forms_path(submitted: 'true')
      else
        #debugger
        #@universal_claim_form.errors.full_messages.join.gsub('Patient Contact Information: Value', 'Patient phone number')
        flash.now[:error] = "Form has errors and was unable to be submitted.<br/> " <<
            "<ul><li>#{@universal_claim_form.errors.full_messages.join("</li><li>")}</li></ul>".gsub('\n','<br/>')
        render 'edit'
      end
    rescue UniversalClaimFormPrescriptionDuplicateException => e
      flash[:error] = "Unable to complete.<br />\nThis submission was determined to be a duplicated based on the following:<br />\n#{e.message}"
      render 'edit'
    rescue => e
      logger.error "[UNIVERSAL_CLAIMS_FORM]#{e.message} : #{e.backtrace}"
      flash[:error] = 'There was an error while submitting the form.'
      render 'edit'
    end


  end

I tried adding the line @universal_claim_form.errors.full_messages.join.gsub('Patient Contact Information: Value', 'Patient phone number') (commented out above) to get the error message, convert to a string, and then replace the incorrect part with the correct message, but this doesn't work.

Any ideas?

Aucun commentaire:

Enregistrer un commentaire