以下是ERB教程中的代码。当我尝试执行代码时,编译器抱怨说“(erb):16:未定义的局部变量或方法`priority'用于main:Object(NameError)”。我无法弄清楚原因。有人可以帮帮我吗?
require "erb"
# Create template.
template = %q{
From: James Edward Gray II <james@grayproductions.net>
To: <%= to %>
Subject: Addressing Needs
<%= to[/\w+/] %>:
Just wanted to send a quick note assuring that your needs are being
addressed.
I want you to know that my team will keep working on the issues,
especially:
<%# ignore numerous minor requests -- focus on priorities %>
% priorities.each do |priority|
* <%= priority %>
% end
Thanks for your patience.
James Edward Gray II
}.gsub(/^ /, '')
message = ERB.new(template, 0, "%<>")
# Set up template data.
to = "Community Spokesman <spokesman@ruby_community.org>"
priorities = [ "Run Ruby Quiz",
"Document Modules",
"Answer Questions on Ruby Talk" ]
# Produce result.
email = message.result
puts email
答案 0 :(得分:0)
ERB模板看起来很糟糕,这是由缩进引起的问题。你只需要解决中间问题:
<% priorities.each do |priority| %>
* <%= priority %>
<% end %>
备用语法是在行的开头处设置%
。在您的情况下,您无意中添加了一些空格,这些空格使ERB的该部分无效。