0

いくつかのパラメーターを DB に解析し、URL から画像を解析する必要があります。

画像にはペーパークリップを使用しています。

Rails コンソールでは、次のコードで新しい投稿に画像を追加できます。

    image = Image.new
    image.image_from_url "http://yug-avto.ru/files/image/tradein/hyundai/877_VOLKSWAGEN_FAETON_2011_2_1366379491.jpg"
    image.watermark = true
    image.save!

私が持っている私の画像モデルで

require "open-uri"
.......
def image_from_url(img_url)
  self.image = open(img_url)
end

そして、すべての作業が完了しました。しかし、ノコギリを使用すると、このコードは機能しません。

rake aborted!
No such file or directory - 
http://yug-avto.ru/files/image/tradein/peugeot/1027_Peugeot_308_2011_2_1370850441.jpg

Nokogiri parse のレーキ タスク:

doc.xpath("//item").each do |ad|
 img = ad.at("image").text

 img1 = Image.new
 img1.image = open("#{img}") 
 img1.watermark = true
 img1.save!
end

Nokogiri の rake タスクでは、require 'nokogiri' と require 'open-uri' があります。

どのように?:))))

4

2 に答える 2

3

これは私のパーサーのコード スニペットopen(url)ですparse(url)

picture = Picture.new(
    realty_id: realty.id,
    position: position,
    hashcode: realty.hashcode
)
# picture.image = URI.parse(url), edit: added open() as this worked for Savroff
picture.image = open(URI.parse(url))
picture.save!

さらに、画像が実際に存在するかどうかを確認することをお勧めします

picture_array.each do |url|

    # checks if the Link works
    res = Net::HTTP.get_response(URI.parse(url))

    # if so, it will add the Picture Link to the verified Array
    if res.code.to_i >= 200 && res.code.to_i < 400 #good codes will be betweem 200 - 399
        verified_array << url
    end
end
于 2013-06-10T13:26:52.490 に答える
3

TheChamp に感謝します。あなたは私を正しい考えに導きました。最初に URL を解析してから開く必要があります。

image = Image.new
ad_image_url = URI.parse("#{img}")
image.image = open(ad_image_url)
image.watermark = true
image.save!
于 2013-06-11T07:11:52.867 に答える