我正在构建一个 sinatra 应用程序,它将使用 Highrise CRM gem 访问 Highrise 数据。这个 gem 基于 ActiveResource 类。我想为每个请求设置站点、用户字段。我遵循了这里发布的建议 -基于每个用户设置活动资源 HTTP 身份验证是否线程安全?。我添加代码(如下所示)并收到错误。任何人都可以帮助理解这个错误以及如何修复它。
class ActiveResource::Base
class << self
%w(site user).each do |attr|
define_method(attr) do
Thread.current["active_resource.#{attr}"]
end
define_method("#{attr}=", val) do
Thread.current["active_resource.#{attr}"] = val
end
end
end
end
和错误:
c:/dev/hgadget/application.rb:18:in `block in singletonclass':
undefined local variable or method `val' for #<Class:ActiveResource::Base> (NameError)
from c:/dev/hgadget/application.rb:12:in `each'
from c:/dev/hgadget/application.rb:12:in `singletonclass'
from c:/dev/hgadget/application.rb:11:in `<class:Base>'
from c:/dev/hgadget/application.rb:9:in `<top (required)>'
from <internal:lib/rubygems/custom_require>:29:in `require'
from <internal:lib/rubygems/custom_require>:29:in `require'
from application_test.rb:1:in `<main>'
- - - - - - - - - - - - 更新 - - - - - - - - - - - - - ----
我尝试了你的建议,我现在收到此错误。
NoMethodError - undefined method `path' for "https://test.abcd.com":String:
c:/Ruby192/lib/ruby/gems/1.9.1/gems/activeresource3.0.11/lib/active_resource/base.rb:562:in `prefix'
c:/Ruby192/lib/ruby/gems/1.9.1/gems/activeresource3.0.11/lib/active_resource/base.rb:667:in `collection_path'
c:/Ruby192/lib/ruby/gems/1.9.1/gems/activeresource3.0.11/lib/active_resource/base.rb:856:in `find_every'
c:/Ruby192/lib/ruby/gems/1.9.1/gems/activeresource-3.0.11/lib/active_resource/base.rb:777:in `find' application.rb:78:in `block in <main>'
c:/Ruby192/lib/ruby/gems/1.9.1/gems/sinatra-1.3.1/lib/sinatra/base.rb:1212:in `call'
c:/Ruby192/lib/ruby/gems/1.9.1/gems/sinatra-1.3.1/lib/sinatra/base.rb:1212:in `block in compile!'
c:/Ruby192/lib/ruby/gems/1.9.1/gems/sinatra-1.3.1/lib/sinatra/base.rb:772:in `[]'
c:/Ruby192/lib/ruby/gems/1.9.1/gems/sinatra-1.3.1/lib/sinatra/base.rb:772:in `block (3 levels) in route!'
c:/Ruby192/lib/ruby/gems/1.9.1/gems/sinatra-1.3.1/lib/sinatra/base.rb:788:in `route_eval'
c:/Ruby192/lib/ruby/gems/1.9.1/gems/sinatra-1.3.1/lib/sinatra/base.rb:772:in `block (2 levels) in route!'
c:/Ruby192/lib/ruby/gems/1.9.1/gems/sinatra-1.3.1/lib/sinatra/base.rb:821:in `block in process_route'
我已经玩了很多设置site
在运行时动态选项,这是我找到的唯一不会导致竞争条件的解决方案。
class Runner
def self.new(site)
Class.new(ActiveResource::Base) do
self.site = site
self.element_name = 'runner'
# your methods here
end.new
end
end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)