使用ActiveResource获取书籍集会返回错误

时间:2015-02-25 09:39:17

标签: ruby-on-rails ruby activeresource

使用activeresource获取图书集。

我有一个从活动资源扩展的类,如下所示:

class BookResource < ActiveResource::Base
  self.site = "http://api.book.com:3000/"
end

这是我的books_controller.rb

class BookController < ApplicationController
  def index
    @book = Book.find(:all)
  end
end

在rails控制台中运行以下命令:

2.1.2 :009 >   BookResource::Book.find(:all)

实际上使用上述调用时,会出现一个名为

的错误
  

“ArgumentError:期望属性Hash,得到[”total_entries“,   38]“

2.1.2 :009 >   BookResource::Book.find(:all)
I, [2015-02-25T14:04:00.492054 #12622]  INFO -- : GET http://localhost:3000/api/v2/books.json
I, [2015-02-25T14:04:00.492197 #12622]  INFO -- : --> 200 OK 5489 (50.0ms)
ArgumentError: expected an attributes Hash, got ["total_entries", 38]
    from /home/diya/.rvm/gems/ruby-2.1.2@verve-offers/gems/activeresource-4.0.0/lib/active_resource/base.rb:1327:in `load'
    from /home/diya/.rvm/gems/ruby-2.1.2@verve-offers/gems/activeresource-4.0.0/lib/active_resource/base.rb:1074:in `initialize'
    from /home/diya/.rvm/gems/ruby-2.1.2@verve-offers/gems/activeresource-4.0.0/lib/active_resource/base.rb:1001:in `new'
    from /home/diya/.rvm/gems/ruby-2.1.2@verve-offers/gems/activeresource-4.0.0/lib/active_resource/base.rb:1001:in `instantiate_record'
    from /home/diya/.rvm/gems/ruby-2.1.2@verve-offers/gems/activeresource-4.0.0/lib/active_resource/base.rb:997:in `block in instantiate_collection'
    from /home/diya/.rvm/gems/ruby-2.1.2@verve-offers/gems/activeresource-4.0.0/lib/active_resource/collection.rb:67:in `block in collect!'
    from /home/diya/.rvm/gems/ruby-2.1.2@verve-offers/gems/activeresource-4.0.0/lib/active_resource/collection.rb:7:in `each'
    from /home/diya/.rvm/gems/ruby-2.1.2@verve-offers/gems/activeresource-4.0.0/lib/active_resource/collection.rb:7:in `each'
    from /home/diya/.rvm/gems/ruby-2.1.2@verve-offers/gems/activeresource-4.0.0/lib/active_resource/collection.rb:67:in `collect!'
    from /home/diya/.rvm/gems/ruby-2.1.2@verve-offers/gems/activeresource-4.0.0/lib/active_resource/base.rb:997:in `instantiate_collection'
    from /home/diya/.rvm/gems/ruby-2.1.2@verve-offers/gems/activeresource-4.0.0/lib/active_resource/base.rb:966:in `find_every'
    from /home/diya/.rvm/gems/ruby-2.1.2@verve-offers/gems/activeresource-4.0.0/lib/active_resource/base.rb:873:in `find'
    from (irb):9
    from /home/diya/.rvm/gems/ruby-2.1.2@verve-offers/gems/railties-4.0.4/lib/rails/commands/console.rb:90:in `start'
    from /home/diya/.rvm/gems/ruby-2.1.2@verve-offers/gems/railties-4.0.4/lib/rails/commands/console.rb:9:in `start'
    from /home/diya/.rvm/gems/ruby-2.1.2@verve-offers/gems/railties-4.0.4/lib/rails/commands.rb:62:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'2.1.2 :010 > 

0 个答案:

没有答案