views:

45

answers:

1

I have a REST API that I am trying to access using Rails 3.0.1 that returns:

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

I have set up my session.rb class defined:

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

The url that will return the XML is http://example.com/rest/sessions.

I then have a controller that is defined as this:

class HomeController < ApplicationController
  def index
    Session.all
  end
end

and my view, index.html.erb, is defined:

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

When I try to view this page, I get the following error:

NoMethodError in HomeController#index

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

What could be causing this issue? I can access the url just fine (even using http://example.com/rest/sessions.xml). Is there a way to debug ActiveResource to see what is happening inside?

EDIT

Here is the stack trace:

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'
A: 

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
Max Schulze
And I would not use a model or class named `Session` in Rails at all, this will lead to a lot of problems, with paths and everything.
Max Schulze
I updated the model name to be `TestSession < ActiveResource::Base`, renamed the file to testsession.rb, and added `self.element_name = 'session'` to the definition. I then tried to return a value from it `@ses = TestSession.all` and got this error: `uninitialized constant HomeController::TestSession`
Steve Wright
Seems like it's not finding your Model, sure you have no typos in their? Is your models in app/models? Or add a require in your controller, like require 'test_session'.
Max Schulze
should the file be renamed to test_session.rb for it to be found?
Steve Wright
Yep. File should be named test_session.rb
Max Schulze
it was the collision of using the variable name "session" in the view - Thanks
Steve Wright