Devise not allowing active resource to access the services

Posted by Saurabh Pandit on Stack Overflow See other posts from Stack Overflow or by Saurabh Pandit
Published on 2011-11-11T13:06:47Z Indexed on 2011/11/12 9:50 UTC
Read the original article Hit count: 444

Filed under:
|

In my application there are two folders one for a rails application and another for a ruby application. In the ruby folder I have created a ruby file in which I have written code to access some model which is present in the rails application using active resource. Sample code is shown below :

active_resource_example.rb

require 'rubygems'
require 'active_resource'

class Website < ActiveResource::Base

  self.site = "http://localhost:3000/admin/"
  self.user = "user"
  self.password = "password"

end

websites = Website.find(:all)
puts websites.inspect

In my rails application I have used ActiveAdmin gem which uses devise for authentication. On rails Server I get the following result :

Started GET "/admin/websites.json" for 192.168.1.37 at 2011-11-12 14:41:06 +0530
  Processing by Admin::WebsitesController#index as JSON
Completed   in 43ms

And on my terminal where I executed active_resource_example.rb, I got following error :

    user@user:~/Desktop$ ruby active_resource_example.rb
/home/user/.rvm/gems/ruby-1.9.2-p180/gems/activeresource-3.1.1/lib/active_resource/connection.rb:132:in `handle_response': Failed.  Response code = 401.  Response message = Unauthorized . (ActiveResource::UnauthorizedAccess)
    from /home/user/.rvm/gems/ruby-1.9.2-p180/gems/activeresource-3.1.1/lib/active_resource/connection.rb:115:in `request'
    from /home/user/.rvm/gems/ruby-1.9.2-p180/gems/activeresource-3.1.1/lib/active_resource/connection.rb:80:in `block in get'
    from /home/user/.rvm/gems/ruby-1.9.2-p180/gems/activeresource-3.1.1/lib/active_resource/connection.rb:218:in `with_auth'
    from /home/user/.rvm/gems/ruby-1.9.2-p180/gems/activeresource-3.1.1/lib/active_resource/connection.rb:80:in `get'
    from /home/user/.rvm/gems/ruby-1.9.2-p180/gems/activeresource-3.1.1/lib/active_resource/base.rb:894:in `find_every'
    from /home/user/.rvm/gems/ruby-1.9.2-p180/gems/activeresource-3.1.1/lib/active_resource/base.rb:806:in `find'
    from active_resource_example.rb:12:in `<main>'
user@user:~/Desktop$

I tried this with another application in which Devise authentication is not used with the same configuration I used in active_resource_example.rb, there I got the result. Desperately need some solution to this issue.

© Stack Overflow or respective owner

Related posts about ruby

Related posts about ruby-on-rails-3