显示 ERB 语法错误的堆栈跟踪。是应用程序代码或 ERB 库的问题?
Stack trace showing ERB syntax error. Is the problem with app code or ERB library?
我正在学习 Lynda.com 课程 Ruby Essential Training Part 3: Files, Formats, Templates 中的 ERB 章节,并且不断收到似乎是 ERB 本身的语法错误。当我输入此代码时:
#!/usr/bin/env ruby
#### Mail Merge ####
#
# Launch this Ruby file from the command line
# to get started
#
require 'erb'
require 'csv'
APP_ROOT = File.expand_path(File.dirname(__FILE__))
# files in current dir
template_path = File.join(APP_ROOT, 'letter_template.txt')
csv_path = File.join(APP_ROOT, 'us_presidents.csv')
template = File.read(template_path)
i = 0
CSV.foreach(csv_path) do |row|
next if row[0].start_with?('Number') # Header row
i += 1
@last_name = row[1]
@first_name = row[2]
state = row[6]
end_date = row[4] || Time.now.year
@title = "The History of #{state}"
@due_date = end_date
years = Time.now.year - @due_date.to_i
@fee = "$#{years * 365}.00"
letter = ERB.new(template).result(binding)
num = i < 10 ? "0#{i}" : i
filename = "letter_#{num}.txt"
filepath = File.join('letters', filename)
puts "-------"
puts filepath
puts letter
File.write(filepath, letter)
end
我收到此错误消息:
Traceback (most recent call last):
15: from init.rb:20:in `<main>'
14: from /Users/andrekibbe/.rvm/rubies/ruby-2.7.0/lib/ruby/2.7.0/csv.rb:510:in `foreach'
13: from /Users/andrekibbe/.rvm/rubies/ruby-2.7.0/lib/ruby/2.7.0/csv.rb:658:in `open'
12: from /Users/andrekibbe/.rvm/rubies/ruby-2.7.0/lib/ruby/2.7.0/csv.rb:511:in `block in foreach'
11: from /Users/andrekibbe/.rvm/rubies/ruby-2.7.0/lib/ruby/2.7.0/csv.rb:1280:in `each'
10: from /Users/andrekibbe/.rvm/rubies/ruby-2.7.0/lib/ruby/2.7.0/csv.rb:1280:in `each'
9: from /Users/andrekibbe/.rvm/rubies/ruby-2.7.0/lib/ruby/2.7.0/csv/parser.rb:336:in `parse'
8: from /Users/andrekibbe/.rvm/rubies/ruby-2.7.0/lib/ruby/2.7.0/csv/parser.rb:823:in `parse_quotable_loose'
7: from /Users/andrekibbe/.rvm/rubies/ruby-2.7.0/lib/ruby/2.7.0/csv/parser.rb:49:in `each_line'
6: from /Users/andrekibbe/.rvm/rubies/ruby-2.7.0/lib/ruby/2.7.0/csv/parser.rb:49:in `each_line'
5: from /Users/andrekibbe/.rvm/rubies/ruby-2.7.0/lib/ruby/2.7.0/csv/parser.rb:52:in `block in each_line'
4: from /Users/andrekibbe/.rvm/rubies/ruby-2.7.0/lib/ruby/2.7.0/csv/parser.rb:871:in `block in parse_quotable_loose'
3: from /Users/andrekibbe/.rvm/rubies/ruby-2.7.0/lib/ruby/2.7.0/csv/parser.rb:1122:in `emit_row'
2: from init.rb:34:in `block in <main>'
1: from /Users/andrekibbe/.rvm/rubies/ruby-2.7.0/lib/ruby/2.7.0/erb.rb:905:in `result'
/Users/andrekibbe/.rvm/rubies/ruby-2.7.0/lib/ruby/2.7.0/erb.rb:905:in `eval': (erb):6: syntax error, unexpected '=', expecting end-of-input (SyntaxError)
; = @due_date ; _erbout.<< "\n\n...**
堆栈跟踪中的所有内容都引用了 CSV 和 ERB 行,所以我看不到任何可以在我这边更正的内容。我错过了什么吗?
更新: letter_template.rb 添加:
Dear <%= @first_name %> <%= @last_name %>,
The following library book is overdue.
"<%= @title %>"
Due: <% = @due_date %>
Overdue fee: <%= @fee %>
Thank you for your prompt attention to this matter.
堆栈跟踪显示来自 ERb 的行,因为 ERb 代码是正在执行的代码。
虽然理论上问题可能出在 ERb 本身或您的模板中,但鉴于 ERb 被成千上万的开发人员使用,更有可能问题出在您的模板中和每日申请。
我正在学习 Lynda.com 课程 Ruby Essential Training Part 3: Files, Formats, Templates 中的 ERB 章节,并且不断收到似乎是 ERB 本身的语法错误。当我输入此代码时:
#!/usr/bin/env ruby
#### Mail Merge ####
#
# Launch this Ruby file from the command line
# to get started
#
require 'erb'
require 'csv'
APP_ROOT = File.expand_path(File.dirname(__FILE__))
# files in current dir
template_path = File.join(APP_ROOT, 'letter_template.txt')
csv_path = File.join(APP_ROOT, 'us_presidents.csv')
template = File.read(template_path)
i = 0
CSV.foreach(csv_path) do |row|
next if row[0].start_with?('Number') # Header row
i += 1
@last_name = row[1]
@first_name = row[2]
state = row[6]
end_date = row[4] || Time.now.year
@title = "The History of #{state}"
@due_date = end_date
years = Time.now.year - @due_date.to_i
@fee = "$#{years * 365}.00"
letter = ERB.new(template).result(binding)
num = i < 10 ? "0#{i}" : i
filename = "letter_#{num}.txt"
filepath = File.join('letters', filename)
puts "-------"
puts filepath
puts letter
File.write(filepath, letter)
end
我收到此错误消息:
Traceback (most recent call last):
15: from init.rb:20:in `<main>'
14: from /Users/andrekibbe/.rvm/rubies/ruby-2.7.0/lib/ruby/2.7.0/csv.rb:510:in `foreach'
13: from /Users/andrekibbe/.rvm/rubies/ruby-2.7.0/lib/ruby/2.7.0/csv.rb:658:in `open'
12: from /Users/andrekibbe/.rvm/rubies/ruby-2.7.0/lib/ruby/2.7.0/csv.rb:511:in `block in foreach'
11: from /Users/andrekibbe/.rvm/rubies/ruby-2.7.0/lib/ruby/2.7.0/csv.rb:1280:in `each'
10: from /Users/andrekibbe/.rvm/rubies/ruby-2.7.0/lib/ruby/2.7.0/csv.rb:1280:in `each'
9: from /Users/andrekibbe/.rvm/rubies/ruby-2.7.0/lib/ruby/2.7.0/csv/parser.rb:336:in `parse'
8: from /Users/andrekibbe/.rvm/rubies/ruby-2.7.0/lib/ruby/2.7.0/csv/parser.rb:823:in `parse_quotable_loose'
7: from /Users/andrekibbe/.rvm/rubies/ruby-2.7.0/lib/ruby/2.7.0/csv/parser.rb:49:in `each_line'
6: from /Users/andrekibbe/.rvm/rubies/ruby-2.7.0/lib/ruby/2.7.0/csv/parser.rb:49:in `each_line'
5: from /Users/andrekibbe/.rvm/rubies/ruby-2.7.0/lib/ruby/2.7.0/csv/parser.rb:52:in `block in each_line'
4: from /Users/andrekibbe/.rvm/rubies/ruby-2.7.0/lib/ruby/2.7.0/csv/parser.rb:871:in `block in parse_quotable_loose'
3: from /Users/andrekibbe/.rvm/rubies/ruby-2.7.0/lib/ruby/2.7.0/csv/parser.rb:1122:in `emit_row'
2: from init.rb:34:in `block in <main>'
1: from /Users/andrekibbe/.rvm/rubies/ruby-2.7.0/lib/ruby/2.7.0/erb.rb:905:in `result'
/Users/andrekibbe/.rvm/rubies/ruby-2.7.0/lib/ruby/2.7.0/erb.rb:905:in `eval': (erb):6: syntax error, unexpected '=', expecting end-of-input (SyntaxError)
; = @due_date ; _erbout.<< "\n\n...**
堆栈跟踪中的所有内容都引用了 CSV 和 ERB 行,所以我看不到任何可以在我这边更正的内容。我错过了什么吗?
更新: letter_template.rb 添加:
Dear <%= @first_name %> <%= @last_name %>,
The following library book is overdue.
"<%= @title %>"
Due: <% = @due_date %>
Overdue fee: <%= @fee %>
Thank you for your prompt attention to this matter.
堆栈跟踪显示来自 ERb 的行,因为 ERb 代码是正在执行的代码。
虽然理论上问题可能出在 ERb 本身或您的模板中,但鉴于 ERb 被成千上万的开发人员使用,更有可能问题出在您的模板中和每日申请。