gpt4 book ai didi

html - 在 rails 中输出包含 html 代码的变量 Ruby on Rails

转载 作者:数据小太阳 更新时间:2023-10-29 08:32:42 26 4
gpt4 key购买 nike

我在 ruby​​ 和 rails 中的输出有一点问题。我仍然是 Rails 的初学者,所以解决方案可能非常简单,但我看不到它。

我正在尝试解析一个网站并将一些源代码放在我自己的网站上。问题:它总是将整个源代码作为文本输出,而不是解释 html 代码。我该如何改变它?

代码:

page = Nokogiri::HTML(open("https://www.google.ch/search?client=ubuntu&channel=fs&q=google&ie=utf-8&oe=utf-8&redir_esc=&ei=WMpyUfSWEuz07AaTioCwDw"))     

source = page.css('div#_css0')

<%= page %>

结果:

http://postimg.org/image/dsaib9lx3/

我希望它看起来像:

http://postimg.org/image/z9qlhoef5/

感谢您的任何建议!

最佳答案

你应该在 erb 中使用 raw。它实际上等同于在其上调用 html_safe,但是,就像 h 一样,在 helper 上声明,因此它只能在 Controller 和 View 上使用。

page = Nokogiri::HTML(open("https://www.google.ch"))
<%= raw page %>

<%= raw Nokogiri::HTML(open("http://www.google.com")) %>

关于html - 在 rails 中输出包含 html 代码的变量 Ruby on Rails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16122957/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com