本文介绍了在没有Rails的情况下调用ERB:未定义的方法“原始"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 ERB引擎生成我的Rails网站页面的离线HTML版本.当用Rails显示时,页面显示的很好,但是我自己无法使用ERB生成(尽管使用相同的ERB模板).

I am using the ERB engine to generate an offline HTML version of a page of my Rails website. The page shows great when shown by Rails, but I have trouble generating with ERB by myself (despite using the same ERB template).

首先,我得到错误 undefined method't',我通过将所有<%= t(...)%> 调用替换为错误来解决了该错误<%= I18n.translate(...)%> .

First I was getting the error undefined method 't' and I solved it by replacing all <%=t(...)%> calls with <%=I18n.translate(...)%>.

现在,我得到了未定义的方法'raw'.我应该用其他东西替换所有<%= raw(...)%> 调用吗?如果是,那是什么?

Now I get undefined method 'raw'. Should I replace all <%=raw(...)%> calls with something else? If yes, what?

推荐答案

raw 在actionpack/action_view库中定义为帮助程序,因此如果没有Rails,您将无法使用它.但是ERB模板显示其输出时没有任何转义:

raw is defined as helper in actionpack/action_view library so that without rails you can't use it. But ERB templating shows its output without any escaping:

require 'erb'
@person_name = "<script>name</script>"
ERB.new("<%= @person_name %>").result # => "<script>name</script>"

因此,出于逃避的目的,有 ERB :: Util#html_escape 方法

And because of this for purpose of escaping there is ERB::Util#html_escape method

include ERB::Util
ERB.new("<%= h @person_name %>").result # => "&lt;script&gt;name&lt;/script&gt;"

这篇关于在没有Rails的情况下调用ERB:未定义的方法“原始"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 12:57