读取 html 文件的内容 AS html 而不是文本
Read the contents of html file AS html not as text
这可能是个愚蠢的问题...我正在加载一个带有 HTML 标记的 HTML 文件,我想在视图中执行该标记,但目前它只显示文本.
查看代码:
<%= @parsed_protip %>
视图的控制器代码:
def show
protip = File.read(File.join("app/assets/protips/" + params[:id] + ".html.erb"))
@parsed_protip = Nokogiri::HTML(protip)
end
我需要视图来呈现文件中的所有 HTML,例如,文件可能如下所示:
<title>Some title</title>
<div class="row">
<%= image_tag "..." %>
</div>
<div class="row">
<p>asdfasdf</p>
</div>
但我得到的输出基本上是一个大文本段落:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Should you buy or rent gear?</title> <meta name="description" content="Interactive calculator to help you figure out whether you should rent or buy your own gear"> </head> <body> <div class="row"> </div> <div class="row"> <div class="col-xs-12 col-sm-8"> <div class="col-xs-8 col-xs-offset-2"> <h1>Should you buy your own snowsports apparel?</h1> <p>Your friend invited you to Tahoe this weekend. You’ve never skied before and you’re excited for the sheer adrenaline rush of flying down some slopes. You leave work early on Thursday to pick up some a...
好吧,Nokogiri::HTML
将文本解析为 Nokogiri::HTML
对象,而不是 HTML 您需要的对象。
相反,将要解析的文件中的所有 Ruby 代码转换为纯 HTML。然后使用 html_safe
将文本转换为有效的 HTML.
这可能是个愚蠢的问题...我正在加载一个带有 HTML 标记的 HTML 文件,我想在视图中执行该标记,但目前它只显示文本.
查看代码: <%= @parsed_protip %>
视图的控制器代码:
def show
protip = File.read(File.join("app/assets/protips/" + params[:id] + ".html.erb"))
@parsed_protip = Nokogiri::HTML(protip)
end
我需要视图来呈现文件中的所有 HTML,例如,文件可能如下所示:
<title>Some title</title>
<div class="row">
<%= image_tag "..." %>
</div>
<div class="row">
<p>asdfasdf</p>
</div>
但我得到的输出基本上是一个大文本段落:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Should you buy or rent gear?</title> <meta name="description" content="Interactive calculator to help you figure out whether you should rent or buy your own gear"> </head> <body> <div class="row"> </div> <div class="row"> <div class="col-xs-12 col-sm-8"> <div class="col-xs-8 col-xs-offset-2"> <h1>Should you buy your own snowsports apparel?</h1> <p>Your friend invited you to Tahoe this weekend. You’ve never skied before and you’re excited for the sheer adrenaline rush of flying down some slopes. You leave work early on Thursday to pick up some a...
好吧,Nokogiri::HTML
将文本解析为 Nokogiri::HTML
对象,而不是 HTML 您需要的对象。
相反,将要解析的文件中的所有 Ruby 代码转换为纯 HTML。然后使用 html_safe
将文本转换为有效的 HTML.