2011-11-11 50 views
4

在我的應用程序中有兩個文件夾,一個用於rails應用程序,另一個用於ruby應用程序。在ruby文件夾中,我創建了一個ruby文件,我在其中編寫代碼以訪問使用活動資源存在於rails應用程序中的某些模型。 示例代碼如下所示:設計不允許活動資源訪問服務

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 

在我的Rails應用程序我都用過它採用設計用於驗證ActiveAdmin寶石。在軌道上服務器我得到以下結果:

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 

我的終端在那裏我執行active_resource_example.rb上,我得到了以下錯誤:

[email protected]:~/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>' 
[email protected]:~/Desktop$ 

我試圖與在設計驗證另一個應用程序沒有使用與active_resource_example.rb中使用的配置相同的配置,我得到了結果。 迫切需要解決這個問題。

+0

錯誤表示它在第11行,但代碼片段沒有11行。是否可以將整個'active_resource_example.rb'作爲github的要點? –

+0

@Vincent Agnello:無法發佈active_resource_example.rb作爲github的要點。但我已經編輯過這個文件並再次發佈它......看看你是否從中得到了一些東西。 –

回答

3

上述話題經過長時間的搜尋之後,我發現這一點:

在config /初始化/ devise.rb,我加了以下幾行:

require 'active_resource' 
config.http_authenticatable = true 

這讓我獲取數據。

相關問題