2013-06-10 3 views
0

URL에서 내 DB 및 이미지에 대한 일부 params를 구문 분석해야합니다.Nokogiri + Paperclip = URL에서 img로 XML 구문 분석

이미지에 클립을 사용합니다. 이 코드로 새 게시물에 이미지를 추가 할 수 있습니다

에서 레일 콘솔 : 내 이미지 모델

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 

이 모든 작업이 완료. 그러나 Nokogiri를 사용할 때이 코드는 작동하지 않습니다. 노코 기리 구문 분석에 대한

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

내 레이크 작업 :

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

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

노코 기리에 대한 레이크 작업에서, 나는 '노코 기리'이 필요하고 '오픈 URI'이 필요했다.

될 방법? :))))

+0

ImageMagick도 사용하고 계십니까? 아니면 클립 만? –

+0

with ImageMagick – Savroff

+0

add 'require "open-uri"'- 로컬 파일뿐만 아니라 HTTP 경로도 열 수 있습니다. –

답변

3

이 내 파서에서 코드 조각입니다 ... 난 당신이 parse(url) 대신 open(url)을 사용하고 잘못된 곳 같아요. 이미지가 정말

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

고마워요) tommorow이 코드를 시도하십시오 – Savroff

3

감사 TheChamp을 존재하는 경우

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! 

은 또한 당신이 바로 생각에 저를지도 확인하는 것이 좋습니다 것입니다. 먼저 URL을 구문 분석 한 다음 그 URL을 열어야합니다.

image = Image.new 
ad_image_url = URI.parse("#{img}") 
image.image = open(ad_image_url) 
image.watermark = true 
image.save! 
+0

들으니 기쁩니다! :) 당신의 필요에 맞게 대답을 업데이트했습니다. –

관련 문제