【问题标题】:Link checker in rails导轨中的链接检查器
【发布时间】:2012-06-15 19:56:28
【问题描述】:

我将以下内容存储在 rake 任务中。基本上,如果来自 url 的响应代码不是 200,我需要将“已发布”属性从 1 更新为 0。当我运行此命令时,我得到: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

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1 net-http


    【解决方案1】:

    如果您有 uri = URI('url'),则需要将其更改为 uri = URI(url)。您当前的代码正在尝试将字符串 'uri' 转换为 URI,而不是使用 uri 变量的内容。

    到达那里后,您还会在product.update_attribute(:publsihed, publsihed) 接近尾声时遇到问题。你有一个错字:'publsihed' 而不是 'published'。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-25
      • 2014-05-30
      • 1970-01-01
      相关资源
      最近更新 更多