ROR import csv file: undefined method - ruby-on-rails

I am adding an import CSV to an online ordering app previous question include ROR import csv file no method error, Adding CSV Import to a ROR application. Thank you to those that have helped in the other questions.
the error:
NoMethodError (undefined method `build_from_csv' for #<Class:0x9ddd82c>):
app/controllers/imports_controller.rb:14:in `block in imports'
/usr/local/ruby/lib/ruby/1.9.1/csv.rb:1761:in `each'
/usr/local/ruby/lib/ruby/1.9.1/csv.rb:1370:in `parse'
app/controllers/imports_controller.rb:10:in `imports'
the imports_controller:
require 'csv'
class ImportsController < ApplicationController
def imports
logger.info "File loaded"
infile = params[:dump][:infile].read
n, errors = 0, []
#archive = []
CSV.parse(infile) do |row|
n += 1
# SKIP: header i.e. first row OR blank row
next if n == 1 or row.join.blank?
imports = Order.build_from_csv(row)
if imports.valid?
imports.save
#archive << row
else
errors << row
end
end
logger.info errors
flash[:success] = "The CSV imported successfully"
respond_to do |format|
format.html # index.html.erb
format.json { render :json => #archive }
end
end
originally I had a models/imports.rb but I moved it to models/order.rb since I am trying to add the csv date to that table:
def self.build_from_csv(row)
#last_level = (row[5].downcase == "yes"
orders = Orders.new(:imports => "#{row[0]}}",
:cart_items => "#{row[1]}",
:cart_items_quantity => "#{row[2]}",
:cart_items_price => "#{row[3]}",
:cart_items_description => "#{row[4]}",
:cart_items_upc => "#{row[5]}",
:cart_items_sku => "#{row[6]}")
# :last_level=> last_level)
return imports
end
for the line with the error:
imports = Order.build_from_csv(row)
I have tried
imports = Imports.build_from_csv(row)
I used Order since that is that table that the cart items get inserted into but no luck. I also tried:
imports = CSV.build_from_csv(row)
...
imports = Infile.build_from_csv(row)
and that caused an Uninitialized Constant. I am open to suggestions for other things to try, Thanks again for the help!

Related

Rails 4: Cookie overflow when importing CSV

I am attempting to analyze CSV before importing into my database but am encountering a ActionDispatch::Cookies::CookieOverflow error.
controller
def bulk_upload_weigh_ins
#response = WeighIn.check_file(params[:file])
redirect_to import_weigh_ins_path, notice: #response
end
model
def self.check_file(file)
status = {
name_error: [],
weigh_in_error: []
}
count = 0
CSV.foreach(file.path, headers: #weigh_in_cols) do |row|
hashed_row = row.to_hash
user = User.find_by(scale_id: hashed_row["scale_id"])
if user == nil
status[:name_error] << [hashed_row["name"], hashed_row["scale_id"]]
elsif user.check_ins.with_no_weigh_ins.length != 1
status[:weigh_in_error] << [hashed_row["name"], hashed_row["scale_id"]]
else
count += 1
end
status[:number_of_success] = count
end
return status
end
I've looked at this post but I'm not sure I should be using doing the same fix. Is there a better way to set this up?
I did the following to fix this:
rails generate active_record:session_migration
rake db:migrate
Changed the line in config/initializers/session_store.rb to: Rails.application.config.session_store :active_record_store
Restarted by server
Restarted my browser (wasn't obvious to me)
Hope this helps someone

Handle failure in rake task without aborting

I have this rake task:
desc "get product image urls from ItemMaster"
task :get_product_image_urls => :environment do
require 'item_master'
ItemMaster.get_image
end
It calls this API method that iterates over several thousand database items like so:
class ItemMaster
include HTTParty
format :xml
base_uri 'https://api.myapi.com/v2'
def self.get_image
#items = Item.all
#items.each do |item|
response = get("/item?upc=#{item.upc}&epl=100&ef=png", :headers => {"username" => "myname", "password" => "mypass"})
image_link = response["items"]["item"]["media"]["medium"]["url"]
item_image = ItemImage.where(:upc => item.upc).first_or_create
item_image.update_attributes(:url => "#{image_link}")
end
end
end
The rake task starts up when I call it, until it hits this error about 22 items in:
undefined method `[]' for nil:NilClass
/Users/name/Rails/SG/lib/item_master.rb:12:in `block in get_image'
/Users/name/Rails/SG/lib/item_master.rb:10:in `each'
/Users/name/Rails/SG/lib/item_master.rb:10:in `get_image'
/Users/name/Rails/SG/lib/tasks/get_product_image_urls.rake:4:in `block in <top (required)>'
/Users/name/.rvm/gems/ruby-1.9.3-p429/bin/ruby_noexec_wrapper:14:in `eval'
/Users/name/.rvm/gems/ruby-1.9.3-p429/bin/ruby_noexec_wrapper:14:in `<main>'
Line 12 is this guy: image_link = response["items"]["item"]["media"]["medium"]["url"] so I'm thinking that probably a url is missing in the api and it's causing the rake task to fail. Is there a way to move past an error like this and continue on with the rest of the rake task? Thanks in advance!
The ItemMaster.get_image method will need to be edited to either not create the exception condition in the first place, or to rescue on a proper exception and move on. For more on exception handling: http://www.tutorialspoint.com/ruby/ruby_exceptions.htm
An example would be:
def self.get_image
#items = Item.all
#items.each do |item|
response = get("/item?upc=#{item.upc}&epl=100&ef=png", :headers => {"username" => "myname", "password" => "mypass"})
begin
image_link = response["items"]["item"]["media"]["medium"]["url"]
item_image = ItemImage.where(:upc => item.upc).first_or_create
item_image.update_attributes(:url => "#{image_link}")
rescue NoMethodError => ex
logger.error "Failed to locate image link ..." # Customize this to your liking
end
end
end
end
For extra goodness, consider handling the code for each item within a separate method so you can isolate responsibility for handling item-related code within the Item model itself!

Import CSV from url Errno::ENAMETOOLONG: file name too long

I'm trying to import a CSV file from a url but i'm getting Errno::ENAMETOOLONG: file name too long. I process the file as follow:
require 'open-uri'
url = "http://de65.grepolis.com/data/csv.txt"
url_data = open(url).read()
SmarterCSV.process(url_data, {
...
})
What am i missing ?
You have to pass a filename which should be on server. rightnow you are passing all data . Do something like this
require 'open-uri'
url = "http://de65.grepolis.com/data/csv.txt"
url_data = open(url).read()
File.open('/tmp/file_name', 'w') { |file| file.write(url_data) }
SmarterCSV.process('/tmp/file_name',{ })
I had the same problem using the standard CSV library to pull in a CSV file via an http url. I was able to solve the issue without needing to write to a temporary server file with code like this:
require 'open-uri'
require 'csv'
url = "http://de65.grepolis.com/data/csv.txt"
url_data = open(url).read()
CSV.parse(url_data, headers: true).each do |row|
# per row processing code ...
end
Hope this helps you.
# models/concerns/import.rb
require 'open-uri'
require 'import_error'
module Import
extend ActiveSupport::Concern
class_methods do
def import_remote(url)
csv = CSV.parse(open(url), headers: true)
begin
ActiveRecord::Base.transaction do
counter = 0
csv.each do |row|
row_hash = row.to_hash
begin
instance = self.name.constantize.create!(row_hash)
rescue => e
raise ImportError.new("#{e.message}. at row: #{row_hash}")
end
counter += 1 if instance.persisted?
end
end
rescue => e
return puts e.message
end
puts "Imported #{counter} records"
end
end
end
# lib/tasks/import.rake
namespace :remote_import do
desc "Import companies from CSV"
task :your_model, [:url] do |t, args|
YourModel.import_remote(args.url)
end
end
# lib/import_error.rb
class ImportError < StandardError
end
# models/your_model.rb
class YourModel < ActiveRecord::Base
include Import
end
Gist: https://gist.github.com/victorhazbun87/9ac786961bbf7c235f76

Rails/unicode issue

I have a bit of my Ruby/Rails (Ruby 2.0.0p195, Rails 3.2.13) project that works as a proxy; that is, you pass it a URL, it goes out and fetches the page, and presents it to you. This generally works as expected, but it seems to munge certain characters (such as è).
A simplified version of the controller is this:
class HomeController < ApplicationController
def geoproxy
require 'net/http'
require 'timeout'
rawurl = CGI::unescape(params[:url])
fixedurl = rawurl.gsub('\\', '%5C') # Escape backslashes... why oh why???!?
r = nil;
status = 200
content_type = ''
begin
Timeout::timeout(15) { # Time, in seconds
if request.get? then
res = Net::HTTP.get_response(URI.parse(fixedurl))
status = res.code # If there was an error, pass that code back to our caller
#page = res.body.encode('UTF-8')
content_type = res['content-type']
end
}
rescue Timeout::Error
#page = "TIMEOUT"
status = 504 # 504 Gateway Timeout We're the gateway, we timed out. Seems logical.
end
render :layout => false, :status => status, :content_type => content_type
end
end
The corresponding view is quite simple:
<%= raw #page %>
When I use this proxy to fetch XML containing an è (for example), I get the following error:
Encoding::UndefinedConversionError in HomeController#geoproxy
"\xE8" from ASCII-8BIT to UTF-8
This error occurs at the following line:
#page = res.body.encode('UTF-8')
If I remove the .encode(), the error is resolved, but my XML contains a placeholder instead of the è.
How can I get my project to display the XML properly?
Could you check if the following code works for you? I was able to fix similar problem of mine with it.
#page = res.body.force_encoding('Windows-1254').encode('UTF-8')

Ruby on rails -prawn & prawnto error

In a ruby on rails application prawn ,prawnto is used to generate pdf raises some error..
def generate_report
generate_report = params[:report_type]
# puts(generate_report)
if generate_report == "1"
# get count of all successful downloads
#total_downloads=StatisticDownload.where("DownloadSuccess=?","1").count
#puts(#total_downloads)
# get all downloads grouped by date
#downloads = StatisticDownload.select("date(Date) as downloaded_date, count(id) as count").where("DownloadSuccess=?","1").group("date(Date)")
respond_to do |format|
format.pdf { render :layout => false }
end
end
Code in generate_report.pdf.prawn
pdf.move_down(30)
book = #downloads.map do |item|
[
item.downloaded_date,
item.count
]
end
pdf.table book, :border_style => :grid,
:row_colors => ["FFFFFF", "DDDDDD"],
:headers => ["downloaded_date", "count"],
:align => { 0 => :left, 1 => :right, 2 => :right, 3 => :right }
/admin/generate_report gives a blank page as output
/admin/generate_report.pdf gives an error
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.map
Extracted source (around line #2):
1: pdf.move_down(30)
2: book = #downloads.map do |item|
3: [
4: item.downloaded_date,
5: item.count
how can i rectify this error
May be worth going to http://railscasts.com/episodes/153-pdfs-with-prawn-revised and following the tutorial, you will have to subscribe to watch if you haven't got a pro subscription though.

Resources