2011-12-24 5 views
2

私はHighriseデータにアクセスするためにHighrise CRM gemを使用するsinatraアプリケーションを構築しています。この宝石はActiveResourceクラスに基づいています。私はサイトごとにリクエストごとにユーザーフィールドを設定したい。私はここに掲示された提案に従った - Is it thread safe to set Active Resource HTTP authentication on a per-user basis?。私はコードを追加する(下記参照)、私はエラーが発生します。誰でもこのエラーを理解し、解決する方法を教えてください。ActiveResourceのサイト/ユーザフィールドを設定してください

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' 

答えて

0

ているときに、その引数が自身define_methodにブロックに引数として渡すと指定することができませんdefine_methodでメソッドを定義します。だから、そのようにセッターメソッドを定義することができます:私は、ランタイムと競合状態につながることはありません、私が見つけた唯一の解決策時に動的にsiteオプションを設定することで多くのことをプレーしてきた

define_method("#{attr}=") do |val| 
    Thread.current["active_resource.#{attr}"] = val 
end 
+0

私はエラーを取得、あなたが言ったことを試してみました - https://abcd.test.com - 「NoMethodErrorのための未定義のメソッド 'パス」『』:文字列:」 – user1112996

1

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 
+0

私はセットアップしようとしていますこの匿名クラスのオブジェクト用のフォームですが、匿名クラスはActiveModelやフォームと連携していません。 'クラス名は空白にすることはできません。匿名クラスが与えられたときに名前引数を与える必要があります。この問題の回避策を見つけましたか?ありがとう。 – dombesz

+0

これは私の使用例ではなく、内部コードからカスタムデータを取り出してポストするだけです。私はREST仕様に従っていないので、スタンドワークフローでどのように動作するのか分かりません。しかし、私はこの回避策に満足していません。 – troex

関連する問題