Rails 如何檢查 URL 是否存在?
        
      WriterShelf™ is a unique multiple pen name blogging and forum platform. Protect relationships and your privacy. Take your writing in new directions. ** Join WriterShelf**
      
        WriterShelf™ is an open writing platform. The views, information and opinions in this article are those of the author.
      
  
          Article info
        
        
              Categories:
            
            
                    
⟩
⟩
                  
            
              Tags:
            
            
              Total: 346 words
          
      
              Like
              
              or Dislike
              
            
          About the Author
          很久以前就是個「寫程式的」,其實,什麼程式都不熟⋯⋯
就,這會一點點,那會一點點⋯⋯
        More to explore
      
       
 
 
 
 
 
 
 



檢查一個網址是不是存在,這是一個很常要用到功能,特別是如果你的網頁允許使用者輸入資料,常常,他所指定的網址久了會不見或是不動了,網路上有很多解法,但是我喜歡以下這種有 time out 的解法,這樣就可以避開有些很慢的網頁整個拖累了你自己網頁的回應,我覺得這樣對使用者的體驗很重要,還有,如果網頁回應太慢,就當他不存在吧⋯⋯ 當然,如果你一定要到這個網址,那就不能用 Time out 的方法的。
這個功能常用在 view,所以我們就寫一個 helper,不多說,放碼:
如果你一定要到這個網址,在 stack overflow 上,這裡有人的建議是只用 status code 不等於 404 來判斷網址是不是存在,我覺得是個很好的參考,要不要用,就由您自己決定了。