ActiveResource model is not getting initialized
Asked Answered
G

3

9

I am looking at using ActiveResource but is now facing a problem that I am unable to figure out myself (was searching the net for a couple of days for the solution by now).

So I have an authentication app sitting at http://localhost:80 and a client on port :85

In my auth app

I have a User model with its controller which follows REST architecture and is set to respond to xml calls.

Here is what I have in my auth app:

models/User.rb

class User < ActiveRecord::Base
end

*controllers/users_controller.rb*

class UsersController < ApplicationController
respond_to :html, :xml, :js

def index
    @users = User.find :all
    respond_with @users
end

def show
    @user = User.find(params[:id])
    respond_with @user
end
.
.
.
end

In a client application

I have a class extending from active resource as follows:

models/user.rb

class User < ActiveResource::Base
    self.site = "http://localhost:80"
end

Here is how I am trying to use it: *controllers/sessions_controller.rb*

class SessionController < ApplicationController

  def home
    @user = User.find(:all)
  end
end

what could go wrong, right?..

But then I am getting the following error:

Started GET "/" for 127.0.0.1 at 2013-09-02 08:33:44 +1200 Processing
by SessionsController#home as HTML Completed 500 Internal Server Error
in 3ms

NameError (uninitialized constant ActiveResource):
app/models/user.rb:1:in <top (required)>'
app/controllers/sessions_controller.rb:4:in
home'

Rendered
/usr/lib/ruby/gems/1.9.1/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_source.erb
(1.6ms) Rendered /usr/lib/ruby/gems/1.9.1/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.erb
(2.7ms) Rendered /usr/lib/ruby/gems/1.9.1/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb
(2.2ms) Rendered /usr/lib/ruby/gems/1.9.1/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (37.4ms)

I am using:

ruby 1.9.3p0 (2011-10-30 revision 33570) [i686-linux]
Rails 4.0.0
activeresource (4.0.0) gem is installed

What could I possibly be doing wrong?
Is it possible that ActiveResource failing to connect to localhost:80 and as a result does not get initialized?

EDIT:

done rvm use 2.0.0 so now ruby version is: ruby 2.0.0p247 (2013-06-27 revision 41674) [i686-linux]

EDIT:

RubyGems Environment:
- RUBYGEMS VERSION: 2.0.7
- RUBY VERSION: 1.9.3 (2011-10-30 patchlevel 0) [i686-linux]
- INSTALLATION DIRECTORY: /usr/lib/ruby/gems/1.9.1
- RUBY EXECUTABLE: /usr/bin/ruby1.9.1
- EXECUTABLE DIRECTORY: /usr/bin
- RUBYGEMS PLATFORMS:
- ruby
- x86-linux
- GEM PATHS:
- /usr/lib/ruby/gems/1.9.1
- /home/dmitry/.gem/ruby/1.9.1
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :backtrace => false
- :bulk_threshold => 1000
- REMOTE SOURCES:
- https://rubygems.org/

Guarneri answered 1/9, 2013 at 20:38 Comment(8)
Have you added the activeresource gem to the Gemfile of your client app or your auth app? or both?Tactful
Yes I have added this gem to both applications.. double-checked it now...Guarneri
If you enter the Rails console (rails console from command line) and you type ActiveResource, what is returned?Headsman
Loading development environment (Rails 4.0.0) 2.0.0 :001 > ActiveResource NameError: uninitialized constant ActiveResource from (irb):1 from /home/dmitry/www/TestClient/vendor/bundle/ruby/2.0.0/gems/railties-4.0.0/lib/rails/commands/console.rb:90:in start' from /home/dmitry/www/TestClient/vendor/bundle/ruby/2.0.0/gems/railties-4.0.0/lib/rails/commands/console.rb:9:in start' from /home/dmitry/www/TestClient/vendor/bundle/ruby/2.0.0/gems/railties-4.0.0/lib/rails/commands.rb:64:in <top (required)>' from bin/rails:4:in require' from bin/rails:4:in `<main>'Guarneri
ActiveResource is not being loaded into your environment. Does it show up if you type bundle list activeresource from command line (from within the environment upon which you're running Rails)?Headsman
bundle list returns: /home/dmitry/www/ac/TestClient/vendor/bundle/ruby/1.9.1/gems/activeresource-4.0.0Guarneri
which is wierd... it shows ruby/1.9.1 when command ruby -v returns 2.0.0... ruby/2.0.0/gems does have activeresource gem in it... could this be a problem? how can I set bundle to use my ruby 2.0.0 folder?Guarneri
added 'gem env' output into my question bodyGuarneri
G
17

I have finally figured this out...

For some reason (I would be grateful if someone can post an explanation why) I had to require active resource manually in my user.rb file.

The correct code to make it work should be:

require 'active_resource'  

class User < ActiveResource::Base  
    self.site = "http://localhost:80"  
end  

P.S
Thank you zeantsoi for your comments, those have led me into a search for reasons of this gem not being loaded.

Guarneri answered 2/9, 2013 at 0:47 Comment(0)
C
16

In your Gemfile add the activeresource gem like so:

gem 'activeresource', require: 'active_resource'

This will make it behave as it should - without having to require it at the beginning of every activeresource model.

Clash answered 27/1, 2014 at 16:0 Comment(0)
C
10

ActiveResource should be loaded in your config/application.rb file:

# config/application.rb
# Pick the frameworks you want:
require 'active_resource/railtie'
require "action_controller/railtie"
...

In this case you will be able to configure ActiveResource later in the same file, for example:

# config/application.rb
module TestClient
  class Application < Rails::Application
    config.active_resource.include_format_in_path = false
    config.active_resource.site = "http://localhost:80"
    ...

This is handy if you want to set some default options for all your ActiveResource models and of course you can override any of these options for some specific model:

# app/models/user.rb
class User < ActiveResource::Base
  self.include_format_in_path = true # append .json at the end of url
end
Cleaver answered 13/12, 2013 at 14:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.