Ruby 仅验证rails中的itunes应用商店和play商店url
我有一个简单的问题,就是使用ruby on rails技术验证url。有很多验证url的解决方案,但没有一个对我有效。因为我只允许play store和app store url保存在数据库中 假设我有一个play store应用程序链接。 这是我的模型:Ruby 仅验证rails中的itunes应用商店和play商店url,ruby,ruby-on-rails-3,validation,Ruby,Ruby On Rails 3,Validation,我有一个简单的问题,就是使用ruby on rails技术验证url。有很多验证url的解决方案,但没有一个对我有效。因为我只允许play store和app store url保存在数据库中 假设我有一个play store应用程序链接。 这是我的模型: class AppStore < ActiveRecord::Base validate :custom private def custom if self.url_name.match /paly.google
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
class AppStore
现在的问题是,当我输入url(如“”)时,也是应用程序的完整url,则验证失败。
示例url如下所示
请帮助我解决这个问题,因为我是rails技术的新手。
谢谢大家。请尝试以下操作:
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
class AppStore