gpt4 book ai didi

ruby - 在 ActiveResource 中设置站点/用户字段

转载 作者:太空宇宙 更新时间:2023-11-03 16:35:19 26 4
gpt4 key购买 nike

我正在构建一个将使用 Highrise CRM gem 访问 Highrise 数据的 sinatra 应用程序。这个 gem 基于 ActiveResource 类。我想为每个请求设置站点、用户字段。我遵循了此处发布的建议 - Is it thread safe to set Active Resource HTTP authentication on a per-user basis? .我添加代码(如下所示)但出现错误。任何人都可以帮助理解这个错误以及如何解决它。

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

关于ruby - 在 ActiveResource 中设置站点/用户字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8623204/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com