4

Rails3.0.1を使用してアクセスしようとしているRESTAPIがあり、次の値を返します。

<Sessions>
  <Session>
    <Title>This is a sample title</Title>
    ...Misc
  </Session>
  <Session>
    <Title>Another Title</Title>
    ...Misc
  </Session>
</Sessions>

定義されたsession.rbクラスを設定しました。

class Session < ActiveResource::Base
  self.site = "http://example.com/rest"
end

XMLを返すURLはhttp://example.com/rest/sessionsです。

次に、次のように定義されたコントローラーがあります。

class HomeController < ApplicationController
  def index
    Session.all
  end
end

私のビューindex.html.erbは次のように定義されています。

<h1>List Sessions</h1>
<% @sessions.each do |session| %>
  <div><%= session.title %></div>
<% end %>

このページを表示しようとすると、次のエラーが発生します。

HomeController#indexのNoMethodError

undefined method `collect!' for #<Hash:0x00000105178af8>

この問題の原因は何ですか?URLに問題なくアクセスできます(http://example.com/rest/sessions.xmlを使用しても)。ActiveResourceをデバッグして内部で何が起こっているかを確認する方法はありますか?

編集

スタックトレースは次のとおりです。

activeresource (3.0.1) lib/active_resource/base.rb:885:in `instantiate_collection'
activeresource (3.0.1) lib/active_resource/base.rb:857:in `find_every'
activeresource (3.0.1) lib/active_resource/base.rb:777:in `find'
activeresource (3.0.1) lib/active_resource/base.rb:803:in `all'
app/controllers/home_controller.rb:4:in `index'
actionpack (3.0.1) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.0.1) lib/abstract_controller/base.rb:150:in `process_action'
actionpack (3.0.1) lib/action_controller/metal/rendering.rb:11:in `process_action'
actionpack (3.0.1) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (3.0.1) lib/active_support/callbacks.rb:435:in `_run__140584718597918073__process_action__4423897468290757938__callbacks'
activesupport (3.0.1) lib/active_support/callbacks.rb:409:in `_run_process_action_callbacks'
activesupport (3.0.1) lib/active_support/callbacks.rb:93:in `run_callbacks'
actionpack (3.0.1) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.0.1) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.0.1) lib/active_support/notifications.rb:52:in `block in instrument'
activesupport (3.0.1) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (3.0.1) lib/active_support/notifications.rb:52:in `instrument'
actionpack (3.0.1) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.0.1) lib/action_controller/metal/rescue.rb:17:in `process_action'
actionpack (3.0.1) lib/abstract_controller/base.rb:119:in `process'
actionpack (3.0.1) lib/abstract_controller/rendering.rb:40:in `process'
actionpack (3.0.1) lib/action_controller/metal.rb:133:in `dispatch'
actionpack (3.0.1) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.0.1) lib/action_controller/metal.rb:173:in `block in action'
actionpack (3.0.1) lib/action_dispatch/routing/route_set.rb:62:in `call'
actionpack (3.0.1) lib/action_dispatch/routing/route_set.rb:62:in `dispatch'
actionpack (3.0.1) lib/action_dispatch/routing/route_set.rb:27:in `call'
rack-mount (0.6.13) lib/rack/mount/route_set.rb:148:in `block in call'
rack-mount (0.6.13) lib/rack/mount/code_generation.rb:93:in `block in recognize'
rack-mount (0.6.13) lib/rack/mount/code_generation.rb:68:in `optimized_each'
rack-mount (0.6.13) lib/rack/mount/code_generation.rb:92:in `recognize'
rack-mount (0.6.13) lib/rack/mount/route_set.rb:139:in `call'
actionpack (3.0.1) lib/action_dispatch/routing/route_set.rb:492:in `call'
actionpack (3.0.1) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
actionpack (3.0.1) lib/action_dispatch/middleware/head.rb:14:in `call'
rack (1.2.1) lib/rack/methodoverride.rb:24:in `call'
actionpack (3.0.1) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.0.1) lib/action_dispatch/middleware/flash.rb:182:in `call'
actionpack (3.0.1) lib/action_dispatch/middleware/session/abstract_store.rb:149:in `call'
actionpack (3.0.1) lib/action_dispatch/middleware/cookies.rb:287:in `call'
activerecord (3.0.1) lib/active_record/query_cache.rb:32:in `block in call'
activerecord (3.0.1) lib/active_record/connection_adapters/abstract/query_cache.rb:28:in `cache'
activerecord (3.0.1) lib/active_record/query_cache.rb:12:in `cache'
activerecord (3.0.1) lib/active_record/query_cache.rb:31:in `call'
activerecord (3.0.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:355:in `call'
actionpack (3.0.1) lib/action_dispatch/middleware/callbacks.rb:46:in `block in call'
activesupport (3.0.1) lib/active_support/callbacks.rb:415:in `_run_call_callbacks'
actionpack (3.0.1) lib/action_dispatch/middleware/callbacks.rb:44:in `call'
rack (1.2.1) lib/rack/sendfile.rb:107:in `call'
actionpack (3.0.1) lib/action_dispatch/middleware/remote_ip.rb:48:in `call'
actionpack (3.0.1) lib/action_dispatch/middleware/show_exceptions.rb:46:in `call'
railties (3.0.1) lib/rails/rack/logger.rb:13:in `call'
rack (1.2.1) lib/rack/runtime.rb:17:in `call'
activesupport (3.0.1) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.2.1) lib/rack/lock.rb:11:in `block in call'
<internal:prelude>:10:in `synchronize'
rack (1.2.1) lib/rack/lock.rb:11:in `call'
actionpack (3.0.1) lib/action_dispatch/middleware/static.rb:30:in `call'
railties (3.0.1) lib/rails/application.rb:168:in `call'
railties (3.0.1) lib/rails/application.rb:77:in `method_missing'
railties (3.0.1) lib/rails/rack/log_tailer.rb:14:in `call'
rack (1.2.1) lib/rack/content_length.rb:13:in `call'
rack (1.2.1) lib/rack/handler/webrick.rb:52:in `service'
/Users/swright/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service'
/Users/swright/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run'
/Users/swright/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'
4

3 に答える 3

3

Simple answer, if you try to session in your views it will access the Rails Session object, you have a naming problem on your hands.

Thats why the Hash error is coming, it tries to access the session Hash.

Try something like

<h1>List Sessions</h1>
<% @sessions.each do |s| %>
  <div><%= s.title %></div>
<% end %>

And by the way, you might change your controller to:

class HomeController < ApplicationController
  def index
    @sessions = Session.all
  end
end
于 2010-10-26T18:18:55.797 に答える
1

私はこの問題を抱えていました。上記のJoelのように、タイプが定義されていなかったことが原因でした。ここで解決策を見つけましたが、XMLを制御できない場合は、カスタム形式を定義して、ActiveResourceクラスにそれを使用するように指示できます。

class TaxAlert < ActiveResource::Base
  protected

  class CustomFormat
    include ActiveResource::Formats::XmlFormat

    def decode(xml)
      ActiveResource::Formats::XmlFormat.decode(xml)['alert']
    end
  end

  self.format = CustomFormat.new
end
于 2012-07-25T13:48:50.163 に答える
1

私にとって問題は、タイプがxmlタグで定義されていなかったことです

xml.instruct!
xml.posts("type"=>"array") do
  @posts.each do |p|
    xml.post do 
      xml.id("type"=>"integer") do
        p.id
      end
      xml.title p.title
      xml.content p.content
    end
  end
end

xml.instruct!
xml.posts do
  @posts.each do |p|
    xml.post do 
      xml.id p.id
      xml.title p.title
      xml.content p.content
    end
  end
end

これがお役に立てば幸いです

于 2011-05-25T13:41:45.313 に答える