What is the best way to nest controllers with Rails? Scenario: You have an administrative area. You want to store a series of "tools" (controllers) inside a folder named Admin, for instance: Admin/Users Admin/Products etc. Kyle Heon kheon-Wuw85uim5zDR7s880joybQ@public.gmane.org _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
This should work: class Admin::UsersController < ApplicationController On Jun 15, 2005, at 9:14 PM, Kyle Heon wrote:> What is the best way to nest controllers with Rails? > > Scenario: > > You have an administrative area. You want to store a series of > “tools” (controllers) inside a folder named Admin, for instance: > > Admin/Users > > Admin/Products > > etc… > > Kyle Heon > > kheon-Wuw85uim5zDR7s880joybQ@public.gmane.org > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >---------------------------------------------- Encytemedia.com Professional User Interface Design for Rails Applications _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On 16/06/2005, at 12:14 PM, Kyle Heon wrote:> What is the best way to nest controllers with Rails? > > Scenario: > > You have an administrative area. You want to store a series of “tools” > (controllers) inside a folder named Admin, for instance: > > Admin/Users > > Admin/Products > > etc…Check out typo''s source: svn://leetsoft.com/typo/trunk - tim
Check out how it''s done in Typo. He has multiple regular controllers for public use. For admin he has a BaseController (admin/ base_controller.rb) from which all admin controllers inherit. The inheritance from BaseController (which itself inherits from ApplicationController) lets you accomplish your admin-wide needs, but it prevents the same kind of inheritance approach that Justin is suggesting below. It really depends on what you are trying to do. In Typo''s case there''s not too much from the public side that you can do (apart from comments), so there''s no real need for the admin controllers to inherit from them. Matt http://typo.leetsoft.com On Jun 15, 2005, at 10:18 PM, Justin Palmer wrote:> This should work: > > class Admin::UsersController < ApplicationController > > > On Jun 15, 2005, at 9:14 PM, Kyle Heon wrote: > >> What is the best way to nest controllers with Rails? >> >> Scenario: >> >> You have an administrative area. You want to store a series of >> “tools” (controllers) inside a folder named Admin, for instance: >> >> Admin/Users >> >> Admin/Products >> >> etc… >> >> Kyle Heon >> >> kheon-Wuw85uim5zDR7s880joybQ@public.gmane.org >> >> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> > > ---------------------------------------------- > Encytemedia.com > Professional User Interface Design for Rails Applications > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >Matt Pelletier pelletierm-A1PILTyJ15gXhy9q4Lf3Ug@public.gmane.org _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Kyle Heon wrote:> What is the best way to nest controllers with Rails? > > Scenario: > > You have an administrative area. You want to store a series of “tools” > (controllers) inside a folder named Admin, for instance: > > Admin/Users > > Admin/Products >Admin/Users will be Admin::UsersController Admin/Users/Bob will be Admin::Users::BobController But you should be careful if you have controllers like, /A/action/id => AController /A/B/action/id => A::BController /A/B/C/action/id => A::B::CController because for the second (and 3rd) one, the current rails implementation will try to call [:controller => AController, :action => B, :id => action], and give up on the id. It matches the first possible thing instead continues to match controllers until it can match no more. :) If you have nested controllers like that, use the patch I submitted some time ago @ http://dev.rubyonrails.com/ticket/1375. Use the routing.4.diff since that is the one that will work. - Adam
Matt Pelletier wrote:> Check out how it''s done in Typo. He has multiple regular controllers > for public use. For admin he has a BaseController > (admin/base_controller.rb) from which all admin controllers inherit. > The inheritance from BaseController (which itself inherits from > ApplicationController) lets you accomplish your admin-wide needs, but > it prevents the same kind of inheritance approach that Justin is > suggesting below. > > It really depends on what you are trying to do. In Typo''s case there''s > not too much from the public side that you can do (apart from > comments), so there''s no real need for the admin controllers to > inherit from them.This is how I structured all the stuff I do as well... works very well... _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails