2011-09-27 16 views
1

我是ROR世界的新手,我嘗試使用ActiveAdmin爲藝術家作品集網站設計管理面板。 這個想法是,每個藝術家都有一個登錄名/密碼並可以管理資產。ActiveAdmin的設計問題

模型在AdminUser表中用has_many設置,在鏈接模型中用belongs_to設置。 例如,AdminUser has_many視頻。 有很多鏈接資產。

什麼是最好的辦法,使:

  • 當前登錄的管理用戶只能訪問自己的資產?
  • 對於每個newluyy創建的資產,當前登錄的AdminUser設置爲admin_user_id字段 ?

非常感謝您的幫助!

回答

0

我不確定這個答案是否及時。

Q1。當前登錄的AdminUser只能訪問他自己的資產。

A1。使用cancan作爲您的授權框架。默認情況下,ActiveAdmin不會爲您提供授權功能。所以,關鍵代碼可能看起來像:

# in your app/models/ability.rb 
class Ability 
    include CanCan::Ability 
    def initialize(user) 
    can :read, Asset, :admin_user_id => user.id 
    end 
end 

使用慘慘(很容易,你可以解決它在15分鐘內)的更詳細的步驟,請參考:https://github.com/ryanb/cancan

Q2。當前登錄的AdminUser被設置爲每個newluyy創建的資產的admin_user_id字段。

A2:AdminUser只是一個「用戶」模型。因此,您可以在視圖和控制器中將其稱爲「current_user」,就像常規設計一樣。例如

#in your controller 
def create_xx 
    # save the admin_user_id to the newly created asset. 
    Asset.create(:admin_user_id => current_user.id) 
end 

爲設計的更多細節,請參見其官方網站:https://github.com/plataformatec/devise