Do not allow the active resource to access services

In my application there are two folders for the rails application and the other for the ruby โ€‹โ€‹application. In the ruby โ€‹โ€‹folder, I created a ruby โ€‹โ€‹file in which I wrote code to access some model that is present in the rails application using an active resource. A 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 used the ActiveAdmin gem, which uses an authentication program. On the 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 the 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 that I used in active_resource_example.rb, there I got the result. Desperately need some solution to this problem.

+4
source share
1 answer

After a long search on the above topic, I found the following:

In config / initializers / devise.rb, I added the following lines:

 require 'active_resource' config.http_authenticatable = true 

This allowed me to get the data.

+3
source

Source: https://habr.com/ru/post/1380766/


All Articles