関連する db テーブルを持たない Permission Model があり、認可に使用されます。before_filter を使用すると、authorize メソッドは、ユーザーとオプションの test_id (別のモデル) に応じて、新しい許可オブジェクトを作成します。アイデアは、テストがユーザーに属しているかどうかを確認し、このユーザーがこのテストを削除できるようにし、そうでない場合はトランザクションをキャンセルすることです。だから私の初期化方法:
class Permission
def initialize(user, *test_id)
@user = user
if !test_id.empty?
test_id.each do |t|
test = Test.find_by_id(t) #finding real test record
self.instance_variable_set(:"@test_#{t}", test) #should set @test_{id}
#an instance of this new permission refering to actual test record
end
end
allow :users, [:new,:create]
allow :root,[]
allow :session, [:create, :destroy]
allow :tests, [:new, :create]
if !test_id.empty?
for i in 0...test_id.length
t = self.instance_variable_get(:"@test_#{i}") #getting this test record.
if t.user_id == @user.id
allow :tests, [:edit,:lock,:unlock, :destroy ]
end
end
end
end
問題は、レールが取得するものinstance_variable_get
が nil であることです。または、インスタンス @test_{id} を間違って設定したか、取得しました。前もって感謝します