Ich habe die Datei folgende /app/validators/hex_color.rb
in meiner Rails-Anwendung:uninitialized konstant, wenn einschließlich Validator
module Validators
class HexColorValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
unless value =~ /^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/i
record.errors[attribute] << (options[:message] || 'must be a valid CSS hex color code')
end
end
end
end
Und dann in meinem Modell an: /app/models/brand_theme.rb
ich habe:
class BrandTheme < ApplicationRecord
include Validators
validates :brand_1, presence: true, hex_color: true
end
Aber ich bekomme die Fehlermeldung:
uninitialized constant BrandTheme::Validators
Warum ist der Validator nicht enthalten? Ich habe versucht, Server auch zurückzusetzen, aber das gleiche Problem kommt auf.
Sie haben wahrscheinlich 'app/validators' nicht zu' autoload_paths' hinzugefügt. –
@MarekLipka Ich hatte den Eindruck, alles unter 'app' ist in Rails automatisch geladen? Wir haben mehrere andere Ordner darunter, die automatisch geladen werden. – Cameron
Nun, ist es nicht. Sie müssen es manuell tun. –