1

我希望在应用程序<%=yield%> 中呈现一个外部网页rates.appiclife.com。

我试过这个:

我将以下方法放在 pages_controller.rb 中

def fetch_url(url)
    r = Net::HTTP.get_response( URI.parse( url ) )
    if r.is_a? Net::HTTPSuccess
      r.body
    else
      nil
    end
  end

在同一个文件中:

def showexternal
  @snippet = fetch_url "http://rates.appiclife.com/"
end

在 showexternal.html.erb 视图/页面中:

<%= @snippet %>

我收到以下错误:incompatible character encodings: UTF-8 and ASCII-8BIT

甚至有可能做到这一点吗?问题是这些价格会更新并在 excel 文件中接收,所以如果我只是将它们放在 html 表中,需要做很多工作来调整它们。

4

1 回答 1

1

您应该能够将 Nokogiri 的请求响应解析成一个字符串,然后用它做任何你想做的事情。您可以在此处找到快速教程:

http://nokogiri.org/tutorials/parsing_an_html_xml_document.html

于 2013-09-05T02:48:37.047 回答