Results 1 to 16 of 16
Thread: how many controllers?
Mar 27, 2007, 00:29 #1
how many controllers?
I'm starting a new web app (my first) in rails and i have to manage at least two entities with crud operations. I wonder if one controller for the admin part (called "admin") and one for normal users/customers (called "users") is just enough. So i will have for example urls like these for the admin part:
etc. and then....
...and so on.
Is it right or maybe it is better to split it?
Mar 27, 2007, 02:06 #2
Mar 27, 2007, 02:40 #3
Mar 27, 2007, 04:09 #4
...and if the application is in a website so intermixed with static pages how can i organize those staic pages? empty actions (so only rendered .rhtml static templates) in yet another controller like "main_controller.rb" or "site_controller.rb"?
Mar 27, 2007, 04:20 #5
class PostsController < ApplicationController before_filter :login_required, :except => [:index, :show] def index .... end #other actions end
class PostsController < ApplicationController before_filter :login_required, :except => [:index, :show] layout :user_layout def index .... end #other actions private def user_layout (%w(index show).include? self.action_name) ? 'main' : 'admin' end endOriginally Posted by Skyblaze
Mar 27, 2007, 05:13 #6I'm not sure what you mean by this. Care to expand a little?
Mar 27, 2007, 06:20 #7
Mar 27, 2007, 06:35 #8
Mar 29, 2007, 14:35 #9
Mar 30, 2007, 02:33 #10
map.connect 'pages/:pretty_name', :controller => 'pages', :action => 'show'
class PagesController < ApplicationController def show @page = Page.find_by_pretty_name(params[:pretty_name]) end end
Mar 30, 2007, 02:53 #11
Apr 8, 2007, 16:56 #12
- Join Date
- Jul 2004
- Gerodieville Central, UK
- 0 Post(s)
- 0 Thread(s)
have a look at the REST stuff in Rails 1.2. It encourages you to stick to certain actions (namely .... index, show, new, create, update, edit, destroy) in a controller and if you need more, it's a general sign that you need to divide up the site into more controllers. having many controllers is not a bad things, and controllers should be small and nimble. Liek suggested, using separate controllers for separate entities / models is highly encouraged... If there is a lot of shared code, cosider using helpers, or extending from a base set of "abstract" controllers. Partial templates also are good for refactoring views.
Avoid using namespaces for controllers (i.e. subfolders like admin, where you do class Admin::SomethingController). If you need to make sub foldersm avoid namespaces and modify the load_paths config setting (config/environment.rb) instead.
Apr 8, 2007, 23:32 #13
- Join Date
- Mar 2005
- 0 Post(s)
- 0 Thread(s)
So to Summarise:
You should not have more than one controller per model
you can link controllers methods by using the Filter options ie filter_before, filter_ after etc
and you can use the routes.rb file to create prettier URL's or hide the names of the controllers from the URL's
Apr 9, 2007, 03:45 #14
Originally Posted by Magic8ball
Originally Posted by Magic8Ball
May 19, 2007, 05:16 #15
I would like to reopen this thread to issue a problem when we dicide to use rails to manage a static only pages website. I like the idea to use rails to a static only site and i would like to try the method explained above of putting the html and content in a database and create a model/controller to manage all with the help of routes.rb and clean urls permanently stored in a field in the database. In that manner we can manage better the static content and use all the rails facilities and at any time we can upgrade the site with a foreign version of the same site (we have only to add another field in the table with the foreign content/html).
Said that anyway i wonder how can i put all the html/content in the related table field? I'm talking about puttting also the rails layout html/content in the database so if i have to make a foreign version of the site i can "load" the layout with the appropriate language/html/content also for the site's layout.
May 26, 2007, 07:17 #16