1

コントローラSecurity::MyControllerのモデルCompany::Itemにアクセスしています。初期化されていない定数Security::Company::Itemのエラーが発生します。つまり、基本的には、特定のモデルに「Security::」を追加しています。Security :: User(同じモジュールセキュリティのモデル)と言う他のモデルには当てはまりません。これについて考えられる説明は何でしょうか?

4

1 に答える 1

2

これはスコープ解決の問題です。::Company::Item中を使ってみてくださいSecurity::MyController

Ruby言語仕様によると

::Something is a shortcut for Object::Something. The idea is that ::Something 
should look up a constant called Something in the global scope, but since ruby 
doesn't truly have a global scope, it looks it up in the Object class, which is 
the nearest thing in ruby to a global scope.

Security::プレフィックス::は、Rubyがそのコンテキストでデフォルトのスコープ(あなたの場合はスコープ)を適用するのを防ぎます

于 2011-09-03T03:00:21.053 に答える