作爲示例,可以說我正在創建一個Twitter( - )克隆。命名空間管理區域
- A用戶的has_many鳴叫
- 鳴叫屬於用戶
- 用戶可以創建鳴叫
- 用戶可以編輯和刪除自己的微博
- 管理員的用戶c編輯並刪除所有微博
- 用戶誰是管理員可以編輯和刪除誰不是管理員
用戶這是我的鳴叫控制器:
class TweetsController < ActionController
before_action :set_tweet, only: [:edit, :update, :destroy]
before_action :only_admins_and_owner, only: [:edit, :update, :destroy]
def edit
# edits the tweet
end
def update
# updates the tweet
end
def destroy
# destroys the tweet
end
private
### security
def only_admins_and_owner
redirect_to root_url unless current_user.is_admin? || current_user === @tweet.user
end
###
def set_tweet
@tweet = Tweet.find(params[:id])
end
end
我的用戶控制器:
class UsersController < ActionController
before_action :set_user, only: [:edit, :update, :destroy]
before_action :only_admins_and_user, only: [:edit, :update, :destroy]
def edit
# edits the user
end
def update
# updates the user
end
def destroy
# destroys the user
end
private
### security
def only_admins_and_user
redirect_to root_url unless current_user.is_admin? || current_user === @user
end
###
def set_user
@user = User.find(params[:id])
end
end
這裏是我的面板控制器。不知道這是否是正確的做法。你怎麼看待我的命名規則?我還沒有將它稱爲AdminController,因爲通過面板(複數)我指的是管理區域中的多個面板,用戶面板(用於在視圖中顯示所有用戶和提供管理控件)和tweets面板:
class PanelsController < ActionController
before_action :only_admins
def users
@users = User.all
end
def tweets
@tweets = Tweet.all
end
private
### security
def only_admins
redirect_to root_url unless current_user.is_admin?
end
###
end
如果您認爲此控制器設置沒問題,應如何配置我的路線以使用這些操作?
我的路線文件:
MyApp::Application.routes.draw do
resources :users, except: [:index] do
resources :tweets, except: [:index]
end
end
現在,如果我是一個普通用戶,在用戶和微博CRUDing離開,但我應該如何命名空間我的面板控制器上面的工作好嗎?
當管理員在查看用戶在面板控制器的用戶觀看,我想的URL看起來像這樣:
/control_panel/users
/control_panel/tweets
,並在管理方面進行編輯時:
/control_panel/users/12/edit
/control_panel/users/12/tweets/142/edit
但是當用戶正在編輯自己的用戶或鳴叫:
/users/12/edit
/users/12/tweets/142/edit
這是因爲panels
管理視圖與用戶視圖大不相同,但編輯,更新和刪除的功能完全相同,因此我想使用已有的操作。我做對了嗎?不知道我還能做什麼,除了將大量操作添加到面板控制器def_user_update
和def_tweet_update
等等的每一個資源。不覺得很不錯...
那麼我應該如何配置我的路線?
我想我想創建一個可選的命名空間,我的兩個嵌套的路線... 也許一個擔心?如果我這樣做,但是,我得到一個uninitialized constant Panel
錯誤。