0

以下は、ERB チュートリアルのコードです。コードを実行しようとすると、コンパイラは「(erb):16: 未定義のローカル変数またはメソッド `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 %>

別の構文は、行の先頭%に aを付けることです。あなたの場合、ERB のその部分を無効にするいくつかのスペースを誤って追加しました。

于 2011-06-02T15:42:42.497 に答える