我有一个简单的问题就是在rails技术上使用ruby来验证url。有很多用于验证url的解决方案但是对我来说都不起作用。因为我想只允许play store和app store url保存在数据库中
说我有一个Play商店应用链接。 这是我的模特:
class AppStore < ActiveRecord::Base
validate :custom
private
def custom
if self.url_name.match /paly.google.com|itunes.apple.com/
else
self.errors.add(:url_name, 'must be app store url or paly store url')
end
end
end
现在,当我输入网址时,就像&#34; http://play.google.com&#34;也是应用程序的完整网址,然后验证是令人费解的。 示例网址将类似于https://play.google.com/store/apps/details?id=com.viber.voip
请帮我解决这个问题。我是rails技术的新手。 谢谢你们。
答案 0 :(得分:2)
请尝试以下方法:
class AppStore < ActiveRecord::Base
VALID_STORES = ['play.google.com', 'itunes.apple.com']
validate :store_url_format
private
def store_url_format
unless VALID_STORES.any? { |host| url_name.includes?(host) }
errors.add(:url_name, 'must be an AppStore or a PlayStore url')
end
end
end