Instant Gratification #3

Dave says:

I added an action called file_list to the say_controller.rb file

def file_list
  @file_list =  Dir.glob("*")

I wrote a template called file_list.rhtml in the app/views/say directory.

<h1>My Files</h1>
<% for file in @file_list %>
  <li><%= h(file) %></li>
<% end %>

Jim says:

I did the same thing, but I omitted the h helper function call. It seems that even when I created a file called temp& in my current directory, the page still displayed fine. According to the book we might want to use the h method if we expected to see file names with greater-than, less-than or ampersand characters which could mess up the HTML.

<% for file in @files %>
  <li><%= file %></li>
<% end %>

Scott says:

I had to change "for file in @files" to "for @file in @files". I am not on version 1.2 which is probably why.

<% for @file in @files %>
  <li><%= file %></li>
<% end %>

Lawrence says:

Something like the following should be cleaner and more Ruby oriented (I suggest the beginners to spend time learning Ruby, the basics and the idioms at least).

<% if @files %>
<h1>Files or Directories</h1>
  <% @files.each do |file| %>
  <li><%= h(file) %></li>
  <% end %>
<% end %>

(Dave says: I personally prefer for loops in templates. No idea why…)

Andrew Says:

It's worth noting that the Dir in the controller code must be capitalized or you will get an error saying you have an undefined local variable or method.

James Says:

I also did the @files.each method. I like keeping things in the variable.method syntax. I think it keeps the code cleaner and easier to understand. Anyway, here is what I added to the controller:

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

And here is what I added to the view:

  <% @files.each do |file| -%>
    <li>File name: <%= file %>
  <% end -%>

Chans says:

Hi, how r ya?
I'm just a beginner of "Rails". So this question seems to be silly to u guys.
I'm lookin' at files and directories together. I don't like it.
What shoud I do to tell these are files, those are directories?
Is there any way to do that thru the methods?

KenA says [aug-12-2008]:


@things = Dir.glob('*')

returns both files and folders, it´s not bad to check it like:
<% for thing in @things -%>
    <% if File.ftype(thing).eql?('file') %>
      file name is: <%= thing %><br />
    <% else %>
      dir name is: <%= thing %><br />
    <% end %>
<% end %>

MNorton says [08-Feb-2009]

I added some formatting:

    div { float: left; }
    div.fileItem { width: 390px; } { color: #33F; }
    div.file { color: #333; }
    div.size { width: 50px; }
    div.time { width: 230px; } { width: 100px; }
<% @files.each do |file| %>
    <div class="fileItem">
        <div class="size"><%= File.size(file) %></div>
        <div class="time"><%= File.ctime(file) %></div>
        <% if %> <div class="name directory"> <% else %> <div class="name file"> <% end %>
            <%= file %>
    <br clear="all" />
<% end %>
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License