新手问题 常见的 Admin 后台是如何写的?(比如 Routes 和 Controller)

cqcn1991 · 2014年11月10日 · 最后由 xiaoronglv 回复于 2014年11月10日 · 3209 次阅读

看到 Guide 里面有好几种

namespace :admin do
  resources :posts, :comments
end

scope '/admin' do
  resources :posts, :comments
end

各自对应不同的 controller 写法

不知道哪种比较简单,便捷?一般是如何实现的?

namespace :admin do
  resources :posts, :comments
end
# 对应controller在controller/admin/xxx_controller.rb

scope '/admin' do
  resources :posts, :comments
end
# 对应controller在controller/xxx_controller.rb

就是多了一个 admin 空间,看你自己的需求了。 我一般配合 cancancan,就用第二种。

下面是我个人的实现方法。

routes.rb

namespace :admin do
  root 'application#dashboard'

  resources :posts, :replies
end

app/controllers/admin 下定义一个父类

app/controllers/admin/application_controller.rb

class Admin::ApplicationController < ApplicationController
  layout 'admin'
  before_filter :require_sign_in

  def dashboard
  end

  private

  def require_sign_in
    redirect_to sign_in_path unless user_signed_in?
  end
end

然后 Admin 的 controller 都放在 app/controllers/admin 下,并继承这个类

app/controllers/admin/posts_controller.rb

class Admin::PostsController < Admin::ApplicationController
end

我把不同职责的 route 放到不同的文件中了,然后在 config/application.rb

config.paths.config.routes.concat Dir[Rails.root.join("config/routes/*.rb")]

Rails best practice 中有一个比较好的范例

Before

# config/routes.rb
ActionController::Routing::Routes.draw do |map|
  map.resources :posts
  map.resources :comments
  map.logout '/logout', :controller => 'sessions', :action => 'destroy'
  map.login '/login', :controller => 'sessions', :action => 'create'

  map.namespace :developer do |dev|
    dev.resources :posts
    dev.resources :comments
    dev.logout '/logout', :controller => 'sessions', :action => 'destroy'
    dev.login '/login', :controller => 'sessions', :action => 'create'
  end

  map.namespace :admin do |admin|
    admin.resources :posts
    admin.resources :comments
    admin.logout '/logout', :controller => 'sessions', :action => 'destroy'
    admin.login '/login', :controller => 'sessions', :action => 'create'
  end

  map.namespace :api do |api|
    api.resources :posts
    api.resources :comments
    api.logout '/logout', :controller => 'sessions', :action => 'destroy'
    api.login '/login', :controller => 'sessions', :action => 'create'
  end
end

After

The better solution is to split the routes into different files according namespaces.

# config/routes.rb
ActionController::Routing::Routes.draw do |map|
  map.resources :posts
  map.resources :comments
  map.logout '/logout', :controller => 'sessions', :action => 'destroy'
  map.login '/login', :controller => 'sessions', :action => 'create'
end

# config/routes/developer.rb
ActionController::Routing::Routes.draw do |map|
  map.namespace :developer do |dev|
    dev.resources :posts
    dev.resources :comments
    dev.logout '/logout', :controller => 'sessions', :action => 'destroy'
    dev.login '/login', :controller => 'sessions', :action => 'create'
  end
end

# config/routes/admin.rb
ActionController::Routing::Routes.draw do |map|
  map.namespace :admin do |admin|
    admin.resources :posts
    admin.resources :comments
    admin.logout '/logout', :controller => 'sessions', :action => 'destroy'
    admin.login '/login', :controller => 'sessions', :action => 'create'
  end
end

# config/routes/api.rb
ActionController::Routing::Routes.draw do |map|
  map.namespace :api do |api|
    api.resources :posts
    api.resources :comments
    api.logout '/logout', :controller => 'sessions', :action => 'destroy'
    api.login '/login', :controller => 'sessions', :action => 'create'
  end
end

In Rails3, you can set the configs in config/application.rb

config.paths.config.routes.concat Dir[Rails.root.join("config/routes/*.rb")]

参考资料:

http://rails-bestpractices.com/posts/73-split-route-namespaces-into-different-files

需要 登录 后方可回复, 如果你还没有账号请 注册新账号