Step 1: In app/controllers/say_controller.rb

Add the filenames method:

def filenames   
    @files = Dir.glob('')
    return @files
end

Step 2: In app/views/say/filenames.html.erb

Create filenames.html.erb as follows:

Files in the current directory


<p>
    The files in the current directory are: 
</p>
<ul>    
    <% for file in @files %>
    <li><%= file; %></li>
    <% end %>
</ul>

Step 3: In app/controllers/say_controller.rb

Add the filenames method

def filenames   # add the filenames method
    @files = Dir.glob('')
    return @files
end

Step 4: In demo/config/routes.rb

Add the route for filenames.html.erb

get "say/hello"
get "say/goodbye"
get "say/filenames"   # add this route

After making the changes described above to:

1) app/controllers/say_controller.rb

2) app/views/say/filenames.html.erb

3) app/controllers/say_controller.rb and

4) demo/config/routes.rb

5) Stop the server ^C

6) Restart the server

demo> rails server webrick

7) Navigate to http://localhost:3000/say/filenames and refresh the page if necessary.

The list of filenames appears with a bullet before each name. Good luck!

This is the end of this multi-step post.

in app/controllers/say_controller.rb write:


  def hello
    @files = Dir.glob('*')
  end

Nit adds: I tried to create a new def. It failed. Value nil.
Same failure when I’ve put it in def goodbye.
Only in def hello it returns the list of files.

in app/views/say/hello.html.erb write:


<p>
File names are:
<ul>
<% @files.each do |file| %>
    <li><%= file %></li>
<% end %>
</ul>
</p>

Just to make it fancier

<p> File names are:
<ul>
    <% if @files.respond_to?("each") %>
    <% @files.each do |file| %>
        <li><%= file %></li>
    <% end %>
    <% end %>
</ul>
</p>

The original from book works (Ruby 1.9.2)

<p>
File names are:
<ul>
<% for file in @files %>
    <li><%= file %></li>
<% end %>
</ul>
</p>

Originally I had used <%= for with the equal sign which is why I ended up here for the solution.