SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    May 2008
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy Ruby File Operation

    Hi,

    I am new to Ruby and I have a question for you all. Maybe trivial, but nevertheless.

    I have a text file, a.txt, with some data. How can I display its contents as such on the browser using Ruby on Rails?

  2. #2
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you put the file into the root of your application, you can do this in a controller.
    Code:
    File.open("a.txt", "r") {|f| @lines_from_a_txt = f.readlines}
    You'll then be able to do this in a view:
    Code:
    <% for line in @lines_from_a_txt %>
    <%= line -%><br />
    <% end %>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •