You''ll probably have better results if you use the read method.
CSV.parse
is meant to be applied to a String.
From the docs:
*************
parse( str, options = Hash.new ) { |row| ... } click to toggle source
parse( str, options = Hash.new )
This method can be used to easily parse
CSV<http://www.ruby-doc.org/stdlib-1.9.3/libdoc/csv/rdoc/CSV.html>out of a
String <http://www.ruby-doc.org/stdlib-1.9.3/libdoc/csv/rdoc/String.html>.
You may either provide a block which will be called with each row of the
String
<http://www.ruby-doc.org/stdlib-1.9.3/libdoc/csv/rdoc/String.html>in turn,
or just use the returned
Array <http://www.ruby-doc.org/stdlib-1.9.3/libdoc/csv/rdoc/Array.html> of
Arrays (when no block is given).
You pass your str to read from, and an optional options Hash containing
anything
CSV::new()<http://www.ruby-doc.org/stdlib-1.9.3/libdoc/csv/rdoc/CSV.html#method-c-new>understands.
read(path, *options)
Use to slurp a
CSV<http://www.ruby-doc.org/stdlib-1.9.3/libdoc/csv/rdoc/CSV.html>file
into an
Array <http://www.ruby-doc.org/stdlib-1.9.3/libdoc/csv/rdoc/Array.html> of
Arrays. Pass the path to the file and any options
CSV::new()<http://www.ruby-doc.org/stdlib-1.9.3/libdoc/csv/rdoc/CSV.html#method-c-new>understands.
This method also understands an additional
:encoding parameter that you can use to specify the Encoding of the data in
the file to be read. You must provide this unless your data is in
Encoding::default_external().
CSV<http://www.ruby-doc.org/stdlib-1.9.3/libdoc/csv/rdoc/CSV.html>will use
this to determine how to parse the data. You may provide a second
Encoding to have the data transcoded as it is read. For example, encoding:
"UTF-32BE:UTF-8" would read UTF-32BE data from the file but transcode
it to
UTF-8 before
CSV<http://www.ruby-doc.org/stdlib-1.9.3/libdoc/csv/rdoc/CSV.html>parses
it.
readlines(*args)
Alias for
CSV::read()<http://www.ruby-doc.org/stdlib-1.9.3/libdoc/csv/rdoc/CSV.html#method-c-read>
.
table(path, options = Hash.new)
A shortcut for:
CSV.read( path, { headers: true,
converters: :numeric,
header_converters: :symbol }.merge(options) )
*************
On Tuesday, May 1, 2012 12:29:32 PM UTC-4, Ruby-Forum.com User
wrote:>
> Hi, This seems to be a very small error but I cannot find any solution
> to this problem.. I am trying to use the CSV gem in my rails(3.2)
> application(ruby 1.9.3) and I am getting an error
"NoMethodError". My
> controller is:
> require ''csv''
>
> def import
> file = params[:file] <-- error
> CSV.parse(file, :headers => false) do |row|
> Event.new(:Ename => row[0], :Edate => row[1], :Elocation
=>
> row[2], :Edesc => row[3], :Oname => row[4], :Oemail => row[5],
:Odetail
> => row[6])
> end
> end
>
> I am getting this file from a view where the users can upload the csv
> file. I am getting a NoMethodError(undefined method ''nil''
for
> nil:NilClass). My best guess is I am using this method wrong, but then
> my next question is how to parse the Csv data? I want to retrieve the
> file from my view and then parse the data into the database. How can I
> open the file for the same? Is the above method not correct?
>
> --
> Posted via http://www.ruby-forum.com/.
>
On Tuesday, May 1, 2012 12:29:32 PM UTC-4, Ruby-Forum.com User
wrote:>
> Hi, This seems to be a very small error but I cannot find any solution
> to this problem.. I am trying to use the CSV gem in my rails(3.2)
> application(ruby 1.9.3) and I am getting an error
"NoMethodError". My
> controller is:
> require ''csv''
>
> def import
> file = params[:file] <-- error
> CSV.parse(file, :headers => false) do |row|
> Event.new(:Ename => row[0], :Edate => row[1], :Elocation
=>
> row[2], :Edesc => row[3], :Oname => row[4], :Oemail => row[5],
:Odetail
> => row[6])
> end
> end
>
> I am getting this file from a view where the users can upload the csv
> file. I am getting a NoMethodError(undefined method ''nil''
for
> nil:NilClass). My best guess is I am using this method wrong, but then
> my next question is how to parse the Csv data? I want to retrieve the
> file from my view and then parse the data into the database. How can I
> open the file for the same? Is the above method not correct?
>
> --
> Posted via http://www.ruby-forum.com/.
>
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/rubyonrails-talk/-/QNLiQBHzpAAJ.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.