0

我有以下存儲在rake任務中。基本上,我需要從1更新「發佈」屬性爲0;如果從URL響應代碼是不是200。當我運行該拿我得到:undefined method 'request_uri' for #<URI::Generic:0x00000102136408 URL:url>軌道中的鏈接檢查器

desc "Problem products" 
task :update_broken_links => :environment do 
    require 'net/http' 

    Product.find(:all, :conditions =>["published = ? AND feed_id = ?", '1', 820]).each do |product| 
    url = product.url 
    uri = URI('url') 
    response = Net::HTTP.get_response(uri) 
    x = response.code.to_i  

    if x != 200 
     published = '0' 
    else 
     published = product.published 
    end 


    product.update_attribute(:publsihed, publsihed) 

    end 
end 

回答

0

如果你有uri = URI('url')你需要改變它到uri = URI(url)。您當前的代碼嘗試將字符串'uri'轉換爲URI而不是使用uri變量的內容。

一旦你到達那裏,你也會在product.update_attribute(:publsihed, publsihed)附近遇到問題。你有一個錯字:'公開'而不是'發佈'。

+0

完美的作品!謝謝! – Yogzzz