在我的应用程序中有两个文件夹,一个用于 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 gem,它使用 devise 进行身份验证。在 Rails 服务器上,我得到以下结果:
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 的终端上,出现以下错误:
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$
我在另一个应用程序中尝试了此操作,其中没有使用与我在 active_resource_example.rb 中使用的相同配置的 Devise 身份验证,我得到了结果。 迫切需要解决此问题的方法。
最佳答案
在对上述主题进行长时间搜索后,我发现了这个:
在 config/initializers/devise.rb 中,我添加了以下几行:
require 'active_resource'
config.http_authenticatable = true
这让我可以获取数据。
关于ruby - 设计不允许事件资源访问服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8094408/