2014-11-02 23 views
0

我有一個簡單的問題,就是使用ruby on rails技術驗證網址。有很多驗證url的解決方案,但沒有一個適用於我。因爲我想允許只能將商店和應用商店網址保存到數據庫中。驗證只有iTunes應用程序商店,並在軌道上播放商店網址

說我有一個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 

現在是,當我輸入網址,如「http://play.google.com」也應用程序的完整URL,則驗證failling。 一個樣本網址就像https://play.google.com/store/apps/details?id=com.viber.voip

請幫我解決這個問題,因爲我是新的rails技術。 謝謝你們。

回答

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 
+0

感謝噸好友@做工精細spickermann.Its ............. – monsur 2014-11-02 16:12:42