I’m trying to write a custom validation for my store.website. I want the method to check if the website field starts with http:// or https://. This is what I’ve tried:
class Store < ActiveRecord::Base
validate :website_has_correct_format
def website_has_correct_format
self.website = self.website.downcase.start_with?(/https?:\/\//, '')
end
end
This doesn’t work though. For some strange reason it literally just saves the letter t.
What’s the correct way to do this? Also, can I put a :message to it as well?
Don’t modify the value in the validator; validators should validate the existing data without changing it.
The
start_with?method takes a normalStringas an argument, not aRegexp.So, you can implement your validator like this: