.rb文件读取一个文件,而不读取另一个文件-但它们在文件结构中彼此相邻...? (Errno :: ENOENT) [英] .rb file reads one file but not the other - but they are right next to each other in the file structure...? (Errno::ENOENT)

查看:67
本文介绍了.rb文件读取一个文件,而不读取另一个文件-但它们在文件结构中彼此相邻...? (Errno :: ENOENT)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从Jumpstart做EventManager教程。我的问题是它没有读取'form_letter.erb'文件。

I am doing the EventManager tutorial from Jumpstart. My problem is that it is not reading the 'form_letter.erb' file.

我的文件结构如下:

~
    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)

我尝试了一些其他线程中看到的方法,以查看文件从何处运行,返回了一个: home / action 。我尝试将该路径添加到.erb路径,但是没有用。有趣的是,.rb文件正在读取 event_attendees.csv 文件,该文件位于文件结构中不会读取的文件旁边。这里发生了什么?我按照教程显示的那样对文件进行了结构化并编写了路径。我将提供.rb代码以供参考:

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  

更新:

我尝试将路径更改为:

   Filer.read "event_manager/form_letter.erb"

我不再收到错误,但是我也没有任何迹象表明发生了任何事情...我可以说它正在运行.rb文件,因为我得到了 => EventManager初始化了作为终端的输出,没有错误..但是课程指出,在这一点上,如果一切正常,则此代码应创建一个目录来存储每个单独的谢谢字母和我没有收到任何指示,也没有看到文件结构中的任何新目录。这是我第一次做这样的事情,所以我真的不知道自己应该看到什么...

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...

推荐答案

尝试此 File.read('../ form_letter.erb')

这篇关于.rb文件读取一个文件,而不读取另一个文件-但它们在文件结构中彼此相邻...? (Errno :: ENOENT)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆