开发者

Another section in Rails

I saw a lot of discussions about creating another section in Rails 3 but not a complete guide.

I would like to create another section for example

/admin/...

All my previous controllers inherits from

ApplicationController 
开发者_运维技巧

and use

layout/application.html.erb

So now I want every controller that is places in the newly created /admin/... directory to inherit form a different BaseController and use a different layout than the application.html.erb. If that is possible can you provide a guide about which files has to be created in /admin/... which for layout and what I have to place in the route files??

Thanks in advance.


Create the admin directory under your controllers and then have an 'admin' controller (so they inherit the set layout - also useful for forcing authentication etc), eg

class Admin::AdminController < ApplicationController        
  layout 'admin/admin'
end

then have your other controllers in the admin directory extend off the admin controller eg

class Admin::CategoriesController < Admin::AdminController

  def index
    ...
  end

end

You'll need to create an admin folder under your layouts too and the admin.html.erb (or whatever templating engine you're using, layout can obviously be named whatever you like). Views also for the other admin controller methods will need to live under their respective admin folder, eg app/views/admin/categories/index.html.erb (second admin is the name of the controller

You'll also need to add the routes in your routes.rb - assuming Rails 3

namespace :admin do
  root :to => 'admin#index' #default page when accessing /admin
  resources :categories #whatever resources you want
  ...
end


you could add a base_controller.rb in your /admin/ and let your other controllers in /admin/ inherit from Admin::BaseController. Just include a < ApplicationController in your /admin/base_controller.rb.

Now specify the layout in your /admin/base_controller.rb.

For routing you only need to add references available to the generic public. Add a namespace for it:

namespace :admin do
    resouces :xyz
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜