Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sinatra: Undefined method bytesize for Hash [closed]

I'm creating a Sinatra app that takes an uploaded CSV file and puts its contents in a hash. When I reference this hash in my app.rb like so:

hash = extract_values(path_to_filename)

I keep getting this error message:

undefined method `bytesize' for Hash:0x007fc5e28f2b90 #object_id

file: utils.rb location: bytesize line: 335

I read somewhere that this is a Webrick issue. I switched to Thin, the error's the same.

My hash / CSV file is of very small size, so it can't be the issue.

I'm using ruby 1.9.3p374.

Thanks!

like image 868
fullstackplus Avatar asked Jun 06 '13 16:06

fullstackplus


2 Answers

This looks like a duplicate of Undefined method `bytesize' for #<Hash>

Sinatra is expecting a string be returned (i.e. last line) of the route method; you can't just return a straight hash.

like image 85
Luke Antins Avatar answered Nov 15 '22 08:11

Luke Antins


Solved:

1) pass the collection to the view:

get '/file/:filename' do
  filename = params[:filename]
  @rows = extract_values(testfile_path(filename))
  haml :search_term
end

2) iterate over it in the view template (erb / haml):

%ul
 - @rows.each do |hash|
  %li
   Id: #{hash[:id]}, Keyword: #{hash[:keyword]}, Searches: #{hash[:searches]}
like image 44
fullstackplus Avatar answered Nov 15 '22 08:11

fullstackplus