简体   繁体   English

Ruby on Rails打印目录

[英]Ruby on rails Print Directory

I am pretty new to ruby and RoR, so please pardon my noviceness. 我对红宝石和RoR还是很陌生,所以请原谅我的新手。 I want to print complete list of the folders inside a given directory in my rails application. 我想在我的Rails应用程序中的给定目录内打印文件夹的完整列表。 Following is the code I use in my show.html.erb file. 以下是我在show.html.erb文件中使用的代码。

<%= dir = Dir.entries(path_to_my_directory) 
      dir.each do |folder|
      puts folder
      end %>

The Dir.entries method return an array, containing the names of all the folders inside the specified directory. Dir.entries方法返回一个数组,其中包含指定目录内所有文件夹的名称。 I traverse this array and print every single value. 我遍历此数组并打印每个值。 But on my application, this prints the complete array like mentioned below. 但是在我的应用程序上,这将打印出如下所述的完整数组。

[".", "metadata.rb", "attributes", "libraries", "CHANGELOG.md", "recipes", "..", "files", "templates", "providers", "resources", "definitions", "README.md"] 

I tried other ways to traverse an array but the output remains the same. 我尝试了其他方法遍历数组,但输出保持不变。 This code when run individually produces the expected output, but when ran from inside my view, it doesn't seem to work. 单独运行时,此代码会产生预期的输出,但是从我的视图中运行时,它似乎不起作用。 Any suggestions on where I might be going wrong. 关于我可能会出错的任何建议。 Thanks in advance. 提前致谢。

You can't use puts in erb. 您不能在erb中使用puts Find the difference between <% %> (processing the Ruby code) and <%= %> (processing and outputs the enclosed Ruby code). 找到<% %> (处理Ruby代码)和<%= %> (处理并输出随附的Ruby代码)之间的区别。 What you wanted to do is: 您想做的是:

<% 
dir = Dir.entries(path_to_my_directory) 
dir.each do |folder|
%>
  <%= folder %>
<% end %>

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

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