Here is another way of doing it
def concatenate_email
self.email.concat('@asamacm.com') unless self.email =~ /@asamacm.com/
end
質問
I am currently trying to create a sign up form that takes a string containing
"first_last" and adds "@exampledomain.com" to create "first_last@exampledomain.com" and write it to the database.
However, if a user types the full domain, I don't want to add it twice, so I need a method that checks to see if "@exampledomain.com" is already present. I don't want to end up with "first_last@exampledomain.com@exampledomain.com" as it will fail to validate and annoy users.
So far, I have put this into my user model:
before_validation { concatenate_email }
VALID_EMAIL_REGEX = /[a-zA-Z]{1,}[_]{1}[a-zA-Z]{1,}@exampledomain.com/i
validates :email, presence: true,
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
def concatenate_email
@mail = self.email.match('@asamacm.com')
if @mail = nil
self.email.concat('@asamacm.com')
else
end
end
I am new to rails and ruby, so I apologize if my terms are incorrect.
解決
Here is another way of doing it
def concatenate_email
self.email.concat('@asamacm.com') unless self.email =~ /@asamacm.com/
end