tortuga
tortuga

Reputation: 757

Ruby on rails Print Directory

I am pretty new to ruby and RoR, so please pardon my noviceness. I want to print complete list of the folders inside a given directory in my rails application. Following is the code I use in my show.html.erb file.

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

Upvotes: 0

Views: 96

Answers (1)

Grych
Grych

Reputation: 2901

You can't use puts in erb. Find the difference between <% %>(processing the Ruby code) and <%= %>(processing and outputs the enclosed Ruby code). What you wanted to do is:

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

Upvotes: 1

Related Questions