原谅我的问题,但我做错了什么?
<ul>
<% ['red', 'white', 'green'].each do |y| %>
<li><%= "#{y} is a color on the Mexican flag" %></li>
<% end %>
</ul>我得到以下错误:
syntax error, unexpected '<', expecting end-of-input编辑*这是我的完整代码:
require 'erb'
x = 42
template = ERB.new "The value of x is: <%= x %>"
puts template.result(binding)
puts "hello world"
<ul>
<% ['red', 'white', 'green'].each do |y| %>
<li><%= "#{y} is a color on the Mexican flag" %></li>
<% end %>
</ul>错误出现在<来自
<ul>线路
发布于 2014-03-11 08:03:00
我们不能在红宝石代码中使用HTML标记。请阅读http://apidock.com/ruby/ERB,http://ruby-doc.org/stdlib-2.1.1/libdoc/erb/rdoc/ERB.html
这说明了如何在Ruby中使用erb模板。
这是它的工作原理,
['red', 'white', 'green'].each do |y|
temp = ERB.new <<-EOF
<li><%= "#{y} is a color on the Mexican flag" %></li>
EOF
puts temp.result(binding)
end 发布于 2014-03-11 08:14:36
您不能在同一个文件中编写普通Ruby代码和ERB模板。Ruby代码应由Ruby解释,ERB模板应由ERB翻译。您可以在这里使用文档引用模板,以便它成为有效的Ruby字符串,然后用ERB解析它。就像你在前几行中所做的那样。
require 'erb'
x = 42
template = ERB.new "The value of x is: <%= x %>"
puts template.result(binding)
puts "hello world"
template = ERB.new <<'END_TEMPLATE'
<ul>
<% ['red', 'white', 'green'].each do |y| %>
<li><%= "#{y} is a color on the Mexican flag" %></li>
<% end %>
</ul>
END_TEMPLATE
puts template.result(binding)如果您使用Rails,在大多数情况下,您不必手动操作ERB模块。erb模板应该位于app/views/文件夹中,而ruby逻辑应该位于app/models/文件夹或app/controllers/或其他库文件夹中。
https://stackoverflow.com/questions/22319272
复制相似问题