问题
I am doing the EventManager tutorial from Jumpstart. My problem is that it is not reading the 'form_letter.erb' file.
My file structure is as follows:
~
event_manager (folder/located in root directory)
lib (subfolder of event-manager)
event_manager.rb (located in lib)
event_attendees.cvs (located in event_manager)
form_letter.erb (located in event_manager)
form_letter.html (located in event_manager)
full_event_attendees.cvs (located in event_manager)
I have tried some of the ways I saw in other threads to see where the file is running from , one returned: home/action
. I tried adding that path to the .erb path, but it didn't work. Interestingly the .rb file is reading the "event_attendees.csv"
file that is sitting right next to the one it won't read in the file structure. What's going on here? I structured the files and wrote the path exactly as the tutorial showed. I will provide the .rb code for reference:
require "csv"
require "sunlight/congress"
require "erb"
Sunlight::Congress.api_key = "e179a6973728c4dd3fb1204283aaccb5"
def save_thank_you_letters(id, form_letter)
Dir.mkdir("output") unless Dir.exists? ("output")
filename = "output/thanks_#{id}.html"
File.open(filename, 'w') do |file|
file.puts form_letter
end
end
def legislators_by_zipcode(zipcode)
legislators = Sunlight::Congress::Legislator.by_zipcode(zipcode)
end
def clean_zipcode(zipcode)
zipcode.to_s.rjust(5,"0")[0..4]
end
puts "EventManager initialized."
contents = CSV.open "event_attendees.csv", headers: true, header_converters: :symbol
template_letter = File.read "form_letter.erb" #<--not being read
erb_template = ERB.new template_letter
contents.each do |row|
id = row[0]
name = row[:first_name]
zipcode = clean_zipcode(row[:zipcode])
legislators = legislators_by_zipcode(zipcode)
form_letter = erb_template.result(binding)
save_thank_you_letters(id, form_letter)
end
UPDATE:
I have tried changing the path to:
Filer.read "event_manager/form_letter.erb"
And I do not get the error anymore, but I also don't get any indication that anything has happened...I can tell it is running the .rb file because I get the => EventManager initialized
as output from terminal with no errors..but the lesson says that at this point if all is going correctly, this code should be creating a directory to store each individual 'thank you' letter in and I am getting no indication that has happened nor do I see any new directories in my file structure. This is my first time doing something like this so I really don't know what I SHOULD be seeing...
回答1:
Try this File.read('../form_letter.erb')
来源:https://stackoverflow.com/questions/29622122/rb-file-reads-one-file-but-not-the-other-but-they-are-right-next-to-each-othe