URLを返すドメインクラスに静的メソッドがあります。私はそのURLを動的に構築する必要がありますが、g.linkは機能しません。ドメインクラスでgrails g.linkを使用できません
static Map options() {
// ...
def url = g.link(controller: "Foo", action: "bar")
// ...
}
私は、次のエラーを取得:
Apparent variable 'g' was found in a static scope but doesn't refer to a local variable, static field or class. Possible causes:
You attempted to reference a variable in the binding or an instance variable from a static context.
You misspelled a classname or statically imported field. Please check the spelling.
You attempted to use a method 'g' but left out brackets in a place not allowed by the grammar.
@ line 17, column 19.
def url = g.link(controller: "Foo", action: "bar")
^
1 error
は明らかに私の問題は、私は、静的な文脈からg
にアクセスしようとしているということなので、どのように私はこの問題を回避できますか?
これは静的スコープでは行わないでください。インスタンスメソッドを使用するか、さらにはこのコードをサービスに入れて、注入することができます。 – OverZealous
私はこれをやっている時にインスタンスがありません。 – ubiquibacon
さて、私はOverZealousに同意します。このようにするのは本当に悪い考えです。 – Gregg