Jump to content

The ultimate community for Ruby on Rails developers.


Photo

uninitialized constant Product::Excel using rails 4


  • Please log in to reply
3 replies to this topic

#1 lavkesh1607

lavkesh1607

    Passenger

  • Members
  • 6 posts

Posted 21 December 2013 - 06:05 AM

Hi.

 

Please resolve this error:

uninitialized constant Product::Excel

 

This my Product Model:

def self.import(file)
  spreadsheet = open_spreadsheet(file)
  header = spreadsheet.row(1)
  (2..spreadsheet.last_row).each do |i|
    row = Hash[[header, spreadsheet.row(i)].transpose]
    product = find_by_id(row["id"]) || new
    product.attributes = row.to_hash.slice(*Product.accessible_attributes)
    product.save!
  end
end

def self.open_spreadsheet(file)
  case File.extname(file.original_filename)
  when ".csv" then CSV.new(file.path, {})
  when ".xls" then Excel.new(file.path, {})
  when ".xlsx" then Excelx.new(file.path, nil, :ignore)
  else raise "Unknown file type: #{file.original_filename}"
  end
end

Edited by jack, 23 December 2013 - 09:29 AM.
Compressed to one post


#2 james

james

    Guard

  • Moderators
  • 221 posts
  • LocationLeeds, U.K.

Posted 21 December 2013 - 06:39 AM

Your log files will tell you where and why the error is occurring, without seeing your stack trace from the log entries and the lines of code that are called when the error occurs it is very very difficult to help.


  • lavkesh1607 likes this

Programming is just about problem solving!


#3 lavkesh1607

lavkesh1607

    Passenger

  • Members
  • 6 posts

Posted 21 December 2013 - 09:53 AM

hello  james

 

this is my log file tells.Any help.

 

 
Started POST "/product/import" for 127.0.0.1 at 2013-12-21 10:48:01 +0530
Processing by ProductController#import as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"nWk5YR+/WkrcrGppB2yMoOdRl87cOIzhdkeOcQ7l5dg=", "file"=>#<ActionDispatch::Http::UploadedFile:0xa904d18 @tempfile=#<File:/tmp/RackMultipart20131221-3433-w4tvp0>, @original_filename="index (1).xls", @content_type="application/vnd.ms-excel", @headers="Content-Disposition: form-data; name=\"file\"; filename=\"index (1).xls\"\r\nContent-Type: application/vnd.ms-excel\r\n">, "commit"=>"Import"}
Completed 500 Internal Server Error in 3ms
 
NameError (uninitialized constant Product::Excel):
  app/models/product.rb:70:in `open_spreadsheet'
  app/models/product.rb:54:in `import'
  app/controllers/product_controller.rb:19:in `import'
 
 
  Rendered /usr/local/lib/ruby/gems/1.9.1/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/rescues/_source.erb (1.5ms)
  Rendered /usr/local/lib/ruby/gems/1.9.1/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/rescues/_trace.erb (2.5ms)
  Rendered /usr/local/lib/ruby/gems/1.9.1/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (2.4ms)
  Rendered /usr/local/lib/ruby/gems/1.9.1/gems/actionpack-4.0.2/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (35.5ms)


#4 james

james

    Guard

  • Moderators
  • 221 posts
  • LocationLeeds, U.K.

Posted 21 December 2013 - 11:23 AM

From your log file I suspect that the error is coming from

when ".xls" then Excel.new(file.path, {})

I have no idea where or how you have configured the Excel class or where it is coming from, I would look closely at how you have set this up.

 

But it could equally be coming from this line

when ".xlsx" then Excelx.new(file.path, nil, :ignore)

Again the same applies, You would need to check out how you have set up the Excelx class


Programming is just about problem solving!





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users