假設我有一個叫做Animal的模型。該模型包含具有兩種可能狀態的枚舉屬性。如何實施授權?
class Animal < ActiveRecord::Base
enum kind: [ :cat, :dog ]
end
然後在我的控制器中創建不同的實例變量。
class AnimalsController < ApplicationController
def index
@cats = Animal.cat
@dogs = Animal.dog
end
end
在我看來,我有兩個單獨的集合。
<h1>Animals</h1>
<%= render partial: 'animals/cat', collection: @cats, as: :cat %>
<%= render partial: 'animals/dog', collection: @dogs, as: :dog %>
我如何進行授權以編輯第一個集合的資源並且不能編輯第二個集合?
以下方法不起作用,因爲它僅適用於完全一個動作。
before_action :current_user_only, except: [:edit]
那麼,我該如何實現這種授權?
在此先感謝!
你想授權一個用戶編輯*所有* cat記錄,但不是'dog'? –