I have a custom user field to hold the projectid of the project the user is a member of.
Purpose: a user that enters a valid (paying) projectid has access to rich content categories.
Checking whether the projectid entered by the user corresponds to an existing projectid is fairly easy:
# Define the method to check the custom field
def validate_projectid_customfield
# Custom field key and predefined list
custom_field_key = 'projectid'
predefined_list = ["558", "963", "819"]
# Get the user's custom field value
user_field_value = self.custom_fields[custom_field_key]
# If the value is not in the predefined list, raise an error
if !user_field_value.nil? && !user_field_value.empty?
unless predefined_list.include?(user_field_value)
# Raise a validation error that Discourse will understand
WHAT TO DO HERE?
end
end
end
At the moment I have the following code in place of ‘WHAT TO DO HERE?’
self.errors.add(:base, I18n.t("user.project.invalid_value"))
raise ActiveRecord::RecordInvalid.new(self)
But that does not seem to work.
If I take the profile page and fill in a value (right or wrong) the system first reacts with “Saved!” immediately followed by “500 Internal Server Error”.
Maybe it is all due the wrong code at the start?
after_initialize do
add_model_callback(User, :before_save) do
validate_projectid_customfield
end
end
Can anyone help me out or direct me to the right documentation?
Thanks!