Forum: Ruby on Rails simple stuff in rails rhtml

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Af656cca6a3806277fe19a0c606ddab7?d=identicon&s=25 Zac Elston (zaq)
on 2006-01-27 19:29
after playing with the DB interaction I'm moving on to doing some file
IO.  but I'm completly lost as to where to put the code and how to call
it.

I'm trying to get a list of files in a directory and it's not obvious to
me how to do this in Rails via a controller method.

so ruby code is

Dir.entries("testdir").last

and html code is

<%= Dir.entries("testdir").last %>

or

<% mylastfile= Dir.entries("testdir").last %>
<%= mylastfile %>

but how do I get this into the application controller
(./app/controllers/application.rb) so I can say anywhere

<%= mylastfile %>



?
C7669e8b5676f61fdf202230cbcf72d8?d=identicon&s=25 Mikkel Bruun (mikkel)
on 2006-01-27 20:54
zac elston wrote:
> <% mylastfile= Dir.entries("testdir").last %>
> <%= mylastfile %>
>
> but how do I get this into the application controller
> (./app/controllers/application.rb) so I can say anywhere
>
> <%= mylastfile %>
>


class XController < Application

def stuff
@mylastfile= Dir.entries("testdir").last
end

end

stuff.rhtml

<%=mylastfile%>

not testet..
132a94ca65959bda6c74fae54bff2425?d=identicon&s=25 Ezra Zygmuntowicz (Guest)
on 2006-01-27 21:11
(Received via mailing list)
On Jan 27, 2006, at 11:54 AM, Mikkel Bruun wrote:

>
> <%=mylastfile%>
>
> not testet..


Or put it in a before filter in application.rb so it will be
available everywhere;

class ApplicationController < ...

   before_filter :get_last_file
   attr_reader :mylastfile
   protected

   def get_last_file
      @mylastfile = Dir.entries("testdir").last
   end

end

Cheers-
-Ezra Zygmuntowicz
Yakima Herald-Republic
WebMaster
http://yakimaherald.com
509-577-7732
ezra@yakima-herald.com
This topic is locked and can not be replied to.