如何在Rails中写入文档根目录之外的文本文件?

时间:2022-11-23 14:05:16

I want to a list of emails in a text file outside of my repository and doc root in ROR. But I need the application to write to it. What is the best way to accomplish this? My guess is a sym link but I'm not sure.

我想在我的存储库之外的文本文件和ROR中的doc root中列出电子邮件列表。但我需要应用程序写入它。完成此任务的最佳方法是什么?我的猜测是一个sym链接,但我不确定。

I am also curious about where I should keep this text file. It is a list of emails to invite people to use my service.

我也很好奇我应该保留这个文本文件的位置。这是邀请人们使用我的服务的电子邮件列表。

Thanks!

1 个解决方案

#1


The filename that you write to doesn't need to be within your source tree. As long as you have a path to the file and the permissions are correct, you should be able to write to anywhere that you want.

您写入的文件名不需要在源树中。只要您拥有文件的路径并且权限正确,您就应该能够写入所需的任何位置。

After verifying the email, you could do something as simple as:

验证电子邮件后,您可以执行以下操作:

File.open("/home/path/or/something/emails.txt", 'a') {|f| f.write("#{email}\n") }

In terms of whether you should keep it in a text file, if it works for you, why not? You could use a database, but that seems like more overhead than you need to store a list of emails.

关于你是否应该将它保存在文本文件中,如果它适合你,为什么不呢?您可以使用数据库,但这似乎比存储电子邮件列表所需的开销更大。

#1


The filename that you write to doesn't need to be within your source tree. As long as you have a path to the file and the permissions are correct, you should be able to write to anywhere that you want.

您写入的文件名不需要在源树中。只要您拥有文件的路径并且权限正确,您就应该能够写入所需的任何位置。

After verifying the email, you could do something as simple as:

验证电子邮件后,您可以执行以下操作:

File.open("/home/path/or/something/emails.txt", 'a') {|f| f.write("#{email}\n") }

In terms of whether you should keep it in a text file, if it works for you, why not? You could use a database, but that seems like more overhead than you need to store a list of emails.

关于你是否应该将它保存在文本文件中,如果它适合你,为什么不呢?您可以使用数据库,但这似乎比存储电子邮件列表所需的开销更大。