这可能是一个愚蠢的问题......我正在加载一个带有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...
答案 0 :(得分:0)
好吧,Nokogiri::HTML
会根据需要将文本解析为Nokogiri::HTML
对象而不是HTML。
相反,将要解析的文件上的所有Ruby代码转换为纯HTML。然后使用html_safe
将文本转换为有效的HTML。