创建了一个简单的佣金任务,在美国寻找待售自行车。它适用于我想提取标题详细信息,但当我添加其他参数,如位置,价格等,并将其保存到帖子数据库我得到错误无法将哈希投射到字符串
Neils-MacBook-Pro-2:bike_scraper neilpatel$ rake scraper:scrape
rake aborted!
TypeError: can't cast Hash to string
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/connection_adapters/abstract/quoting.rb:76:in `type_cast'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/connection_adapters/sqlite3_adapter.rb:261:in `type_cast'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/connection_adapters/sqlite3_adapter.rb:295:in `block in exec_query'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/connection_adapters/sqlite3_adapter.rb:294:in `map'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/connection_adapters/sqlite3_adapter.rb:294:in `exec_query'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/connection_adapters/abstract/database_statements.rb:68:in `exec_insert'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/connection_adapters/abstract/database_statements.rb:95:in `insert'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/connection_adapters/abstract/query_cache.rb:14:in `insert'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/relation.rb:64:in `insert'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/persistence.rb:503:in `_create_record'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/attribute_methods/dirty.rb:87:in `_create_record'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/callbacks.rb:306:in `block in _create_record'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.1.7/lib/active_support/callbacks.rb:82:in `run_callbacks'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/callbacks.rb:306:in `_create_record'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/timestamp.rb:57:in `_create_record'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/persistence.rb:483:in `create_or_update'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/callbacks.rb:302:in `block in create_or_update'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.1.7/lib/active_support/callbacks.rb:82:in `run_callbacks'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/callbacks.rb:302:in `create_or_update'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/persistence.rb:103:in `save'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/validations.rb:51:in `save'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/attribute_methods/dirty.rb:21:in `save'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/transactions.rb:268:in `block (2 levels) in save'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/transactions.rb:329:in `block in with_transaction_returning_status'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/connection_adapters/abstract/database_statements.rb:201:in `block in transaction'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/connection_adapters/abstract/database_statements.rb:209:in `within_new_transaction'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/connection_adapters/abstract/database_statements.rb:201:in `transaction'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/transactions.rb:208:in `transaction'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/transactions.rb:326:in `with_transaction_returning_status'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/transactions.rb:268:in `block in save'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/transactions.rb:283:in `rollback_active_record_state!'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/transactions.rb:267:in `save'
/Users/neilpatel/Desktop/Rails/bike_scraper/lib/tasks/scraper.rake:53:in `block (3 levels) in <top (required)>'
/Users/neilpatel/Desktop/Rails/bike_scraper/lib/tasks/scraper.rake:38:in `each'
/Users/neilpatel/Desktop/Rails/bike_scraper/lib/tasks/scraper.rake:38:in `block (2 levels) in <top (required)>'
Tasks: TOP => scraper:scrape
(See full trace by running task with --trace)
scraper.rake
namespace :scraper do
desc "Fetch Craigslist posts from 3Taps"
task scrape: :environment do
require 'open-uri' #ruby gem
require 'JSON'
# Set API token and URL
auth_token = "fadddddddd4a23a1cc86e0d"
polling_url = "http://polling.3taps.com/poll"
# Specify request parameters
params = {
auth_token: auth_token,
anchor: 2109915749,
source: "CRAIG",
category_group: "SSSS",
category: "SBIK",
'location.country' => "USA",
retvals: "location,external_url,heading,body,timestamp,price,images,annotations"
}
# Prepare API request
uri = URI.parse(polling_url)
uri.query = URI.encode_www_form(params)
# Submit request -hitting enter
result = JSON.parse(open(uri).read)
# Display results to screen (put string - display information)
#puts result["postings"].second["location"]
#store results in database
# Create new Post
result["postings"].each do |posting|
# Create new Post
@post = Post.new
@post.heading = posting["heading"]
@post.body = posting["body"]
@post.price = posting["price"]
@post.state = posting["location"]
@post.external_url = posting["external_url"]
@post.timestamp = posting["timestamp"]
# Save Post
@post.save
end
end
desc "TODO"
task destroy_all_posts: :environment do
end
end
Posts.db
class CreatePosts < ActiveRecord::Migration
def change
create_table :posts do |t|
t.string :heading
t.text :body
t.decimal :price
t.string :state
t.string :external_url
t.string :timestamp
t.timestamps
end
end
end
答案 0 :(得分:3)
错误来自您的@post.save
行,因此posting
中返回的其中一个字段是Hash
,而不是您期望的String
。它不会是price
,所以只需通过puts
检查所有其他人。
我刚刚运行了您的代码,posting["location"]
是Hash
看起来你只想要那个哈希状态,即。
@post.state = posting["location"]["state"]