简体   繁体   English

Dir在本地工作,不在heroku上

[英]rails Dir working local, not on heroku

In a rails app, if I am working locally, I can read a folder, with its own subfolders and files. 在Rails应用程序中,如果我在本地工作,则可以读取一个包含其自己的子文件夹和文件的文件夹。 in Heroku is not working. 在Heroku中无法正常工作。 any help? 有什么帮助吗?

def upload_multifiles
dir = params[:path]

files_directories = Dir["#{dir}/**/*"]
files = []

files_directories.each do |file_directory|
  if file_directory.include? ".pdf"
    files << file_directory
  end
end

end 结束

the error that i get is: 我得到的错误是:

2017-08-07T08:23:06.984190+00:00 app[web.1]: I, [2017-08-07T08:23:06.984142 #4]  INFO -- : [d25c9aa4-70c2-44c1-a44e-6c0a19a92fd8] Completed 500 Internal Server Error in 3ms (ActiveRecord: 0.6ms)
2017-08-07T08:23:06.983899+00:00 app[web.1]: found_dir
2017-08-07T08:23:06.984787+00:00 app[web.1]: F, [2017-08-07T08:23:06.984747 #4] FATAL -- : [d25c9aa4-70c2-44c1-a44e-6c0a19a92fd8] app/controllers/multiuploader_controller.rb:14:in `open'
2017-08-07T08:23:06.984642+00:00 app[web.1]: F, [2017-08-07T08:23:06.984593 #4] FATAL -- : [d25c9aa4-70c2-44c1-a44e-6c0a19a92fd8]
2017-08-07T08:23:06.984756+00:00 app[web.1]: F, [2017-08-07T08:23:06.984698 #4] FATAL -- : [d25c9aa4-70c2-44c1-a44e-6c0a19a92fd8]
2017-08-07T08:23:06.984697+00:00 app[web.1]: F, [2017-08-07T08:23:06.984647 #4] FATAL -- : [d25c9aa4-70c2-44c1-a44e-6c0a19a92fd8] Errno::ENOENT (No such file or directory @ dir_initialize - /Users/xxxxx/yyyyy/ttttt/zzzzzz):
2017-08-07T08:23:06.984788+00:00 app[web.1]: [d25c9aa4-70c2-44c1-a44e-6c0a19a92fd8] app/controllers/multiuploader_controller.rb:14:in `upload_multifiles'

Thanks a lot in advance 提前谢谢

Albert 阿尔伯特

You can not use the File system on heroku like you can do on your lolcal machine. 您不能像在lolcal机器上那样在heroku上使用文件系统。 You will need to use something like S3 if you want to store files (from uploads). 如果要存储文件(来自上载),则需要使用S3之类的东西。 You can read more about it here: How to use heroku's ephemeral filesystem 您可以在此处了解更多信息: 如何使用heroku的临时文件系统

If you just want to read files that you have shipped with your app: the path /Users/xxxx is a OS X directory structure, which does not exist on heroku. 如果只想读取应用程序附带的文件:路径/ Users / xxxx是OS X目录结构,heroku上不存在。 You could use Rails.root to build paths that work in all environments. 您可以使用Rails.root来构建在所有环境中都可以使用的路径。

In addition: listing files from a path supplied through parameters sounds like a huge security risk to me. 另外:从参数提供的路径中列出文件对我来说听起来像是巨大的安全风险 You should not do this. 您不应该这样做。

Also: there is a more elegant way to create a filtered array, for example: 另外:还有一种更优雅的方法来创建过滤数组,例如:

files = ['a.pdf', 'b.pdf', 'c.txt']
pdfs = files.select { |file| file.ends_with?('.pdf') }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM