Zakoff
Zakoff

Reputation: 12995

convert csv file to table in ruby 1.9

I am using the csv library within ruby 1.9, and trying to follow the example for a table shown here:

Link to faster_csv on github

My code is as follows:

<% require 'csv' %>
<% table = CSV.parse("public/assets/file.csv", :headers => true, :header_converters =>   :symbol) %>

 <%= table %>

But the above returns a blank screen where I am expecting it to show the file.

Also when I try to view table[0].fields, I get the error

undefined method `fields' for nil:NilClass

The following works when reading a csv file however:

<% b = CSV.read("public/assets/khq.csv") %>
<%= b %> 

Is there something I am doing incorrectly with the table example?

Upvotes: 1

Views: 1879

Answers (1)

allenhwkim
allenhwkim

Reputation: 27738

For 1.9.2, Try

CSV.table("public/assets/file.csv")

or

CSV.read("public/assets/file.csv", :headers=>true, :conveters=>:numeric, :header_converters=>:symbol)

FYI, CSV.parse is for string, not for a file. You may take a look at document here at http://ruby-doc.org/stdlib-1.9.2/libdoc/csv/rdoc/CSV.html#method-c-parse

Upvotes: 2

Related Questions