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:

<h1>Files in the current directory</h1>

<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

Step 5: After making the changes described above to:

A) app/controllers/say_controller.rb

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

C) app/controllers/say_controller.rb and

D) demo/config/routes.rb

E) Stop the server in the command window in which you started the server:

^C

F) Restart the server

demo> rails server webrick

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

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

This is the end of this five step post. Good luck!

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.