1
我有幾個奇異的資源在我的應用程序,例如:如何使用權威人士對單一資源實施範圍界定?
# routes.rb
MySite::Application.routes.draw do
resource :thing
end
# things_controller.rb
class ThingsController < ApplicationController
def edit
load_thing
end
def update
load_thing
if @thing.update_attributes(thing_params)
...
else
...
end
end
private
def load_thing
@thing ||= current_user.thing
end
def thing_params
params.require(:thing).permit(...)
end
end
我不知道如何利用權威人士來執行策略作用域(before_action :verify_policy_scoped
已ApplicationController
設置)。
我不知道如何形成我爲奇異資源政策範圍,即:
# thing_policy.rb
class ThingPolicy < ApplicationPolicy
Scope < Scope
def resolve
# What to do here...
# scope => ?
end
end
end
# things_controller.rb
def load_thing
# ...and what to do here
@thing ||= policy_scope(...)
end
...方法
resolve
...應該返回一些種類的結果可以是 迭代。
但是,在單數資源情況下,此迭代條款並不真正有效,因此沒有AR樣式範圍......只是一條記錄。
任何人有什麼建議如何去做到這一點?