0

以下是 ERB 教程中的代码。当我尝试执行代码时,编译器抱怨说“(erb):16: undefined local variable or method `priority' for 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
4

1 回答 1

0

该 ERB 模板看起来很混乱,这是由您的缩进引起的问题。你只需要修复中间:

  <% priorities.each do |priority| %>
    * <%= priority %>
  <% end %>

另一种语法是%在行的最开始有一个。在您的情况下,您无意中添加了一些使 ERB 的那部分无效的空格。

于 2011-06-02T15:42:42.497 回答