samedi 30 août 2014

TypeError: can't cast Array to string


Vote count:

0




I'm getting this error when trying to run a rake task using Nokogiri.


This is the code.



namespace :tops do
desc "Get Tops Description"
task get_description: :environment do
require 'nokogiri'
require 'open-uri'

doc = Nokogiri::HTML(open("http://ift.tt/1qTzPZX"))

price = Array.new
items = Array.new
brands = Array.new
sale_price = Array.new
image = Array.new
item_url = Array.new

price = doc.xpath("//del").collect {|node| node.text.strip}
items = doc.xpath("//div/a/h4").collect {|node| node.text.strip}
brands = doc.xpath("//span[contains(@class,'vendor')]").collect {|node| node.text.strip}
sale_price = doc.xpath("//span[contains(@class, 'price')]/text()").collect {|node| node.text.strip}
image = doc.xpath("/div/a/img/@src").collect {|node| node.text.strip}
item_url = doc.css('div.details a').map { |link| link['href'] }.collect

price.each do |prices|
Tops.create(description: price)
end
end
end


This is the full error.



Rake aborted! TypeError: can't cast Array to string /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract/quoting.rb:76:in type_cast' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/connection_adapters/sqlite3_adapter.rb:261:intype_cast' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/connection_adapters/sqlite3_adapter.rb:295:in block in exec_query' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/connection_adapters/sqlite3_adapter.rb:294:inmap' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/connection_adapters/sqlite3_adapter.rb:294:in exec_query' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract/database_statements.rb:68:in exec_insert' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract/database_statements.rb:95:in insert' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract/query_cache.rb:14:in insert' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/relation.rb:64:in insert' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/persistence.rb:502:in _create_record' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/attribute_methods/dirty.rb:87:in _create_record' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/callbacks.rb:306:in block in _create_record' /Library/Ruby/Gems/2.0.0/gems/activesupport-4.1.5/lib/active_support/callbacks.rb:82:in run_callbacks' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/callbacks.rb:306:in _create_record' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/timestamp.rb:57:in _create_record' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/persistence.rb:482:in create_or_update' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/callbacks.rb:302:in block in create_or_update' /Library/Ruby/Gems/2.0.0/gems/activesupport-4.1.5/lib/active_support/callbacks.rb:82:in run_callbacks' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/callbacks.rb:302:in create_or_update' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/persistence.rb:103:in save' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/validations.rb:51:in save' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/attribute_methods/dirty.rb:21:in save' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/transactions.rb:268:in block (2 levels) in save' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/transactions.rb:329:inblock in with_transaction_returning_status' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract/database_statements.rb:201:in block in transaction' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract/database_statements.rb:209:in within_new_transaction' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract/database_statements.rb:201:in transaction' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/transactions.rb:208:intransaction' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/transactions.rb:326:in with_transaction_returning_status' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/transactions.rb:268:inblock in save' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/transactions.rb:283:in rollback_active_record_state!' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/transactions.rb:267:insave' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.5/lib/active_record/persistence.rb:34:in create' /Users/Kcummings/Sites/rvm_tests/lib/tasks/tops.rake:24:in block (3 levels) in ' /Users/Kcummings/Sites/rvm_tests/lib/tasks/tops.rake:23:in each' /Users/Kcummings/Sites/rvm_tests/lib/tasks/tops.rake:23:inblock (2 levels) in ' /Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/lib/rake/task.rb:240:in call' /Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/lib/rake/task.rb:240:in block in execute' /Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/lib/rake/task.rb:235:in each' /Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/lib/rake/task.rb:235:in execute' /Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/lib/rake/task.rb:179:in block in invoke_with_call_chain' /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/monitor.rb:211:in mon_synchronize' /Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/lib/rake/task.rb:172:in invoke_with_call_chain' /Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/lib/rake/task.rb:165:in invoke' /Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/lib/rake/application.rb:150:in invoke_task' /Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/lib/rake/application.rb:106:in block (2 levels) in top_level' /Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/lib/rake/application.rb:106:in each' /Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/lib/rake/application.rb:106:in block in top_level' /Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/lib/rake/application.rb:115:in run_with_threads' /Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/lib/rake/application.rb:100:in top_level' /Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/lib/rake/application.rb:78:in block in run' /Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/lib/rake/application.rb:176:in standard_exception_handling' /Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/lib/rake/application.rb:75:in run' /Library/Ruby/Gems/2.0.0/gems/rake-10.3.2/bin/rake:33:in' /usr/bin/rake:23:in load' /usr/bin/rake:23:in'




asked 1 min ago







TypeError: can't cast Array to string

Aucun commentaire:

Enregistrer un commentaire