SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    May 2005
    Location
    Sweden
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Best way to "design" the controllers?

    I'm about making a design for my page but before moving on I would like to here some opinions about how you deal with controllers.
    Is it best to not have to many controllers?

    My first idea was to have the following:
    • admin_controller (for admin pages, limited access)
    • public_controller (for public pages)
    • authorize_controller (dealing with the login/logout stuff)


    This seems clean and simple. The disadvantage with this solution is that the public and the admin controll might become big.
    The advantage that I can see is that it easy to see which controller to updated when adding new features.
    Is this a good way to build up the site? As it is now, the controllers are flexible.

    The other way I have been thinking of is to have more specific controller, for example:
    • admin_user
    • admin_ news


    And so on...

    How do you do when you develop, and why?

  2. #2
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can group your controllers into modules, say Admin::UserController, which lives in app/controllers/admin/user_controller.rb

    I think you should keep your controllers as small as possible. They should focus on a specific area of functionality, the basis of any good class design, and controllers are just classes.

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2005
    Location
    Sweden
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That seems like a good idea. I tried it earlier today but I couldn't find any way to access the page.

    app/controllers/admin/user_controller.rb:
    http://mypage/admin/user/

    User is okey because it is a controller. The admin i different though because it is a folder. Do I need to have a admin controller that redirects the request to the folder admin in my controller folder?

  4. #4
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you get an error message?

    http://mypage/admin/user/

    Means that you load Admin::UserController.

    Do you get any error messages?

    I would implement this in another way. I would have one controller for users: UsersController which has several methods, some of them are public, others are admin-only. You can find more information here: http://manuals.rubyonrails.com/read/chapter/49#page142

  5. #5
    SitePoint Enthusiast
    Join Date
    May 2005
    Location
    Sweden
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Interesting I will look on that too.

    I get some kind of routing error. It's seems that it finds the controller but fails on loading it?

    I try to access it by:
    http://mypage/test/mytest

    Test is not a controller it's a folder in my controller folder.
    mytest is a controller in the folder test mentioned above.

    LoadError in <controller not set>#<action not set>

    Already loaded file './script/../config/../app/controllers/test/mytest_controller.rb' but 'MytestController' was not set, perhaps you need to rename './script/../config/../app/controllers/test/mytest_controller.rb'?

    RAILS_ROOT: ./script/../config/..
    Application Trace | Framework Trace | Full Trace
    So it finds it but then something strange happens. Any idea how to get it to work?

  6. #6
    SitePoint Enthusiast
    Join Date
    May 2005
    Location
    Sweden
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No clue, or is it impossible to have folders in the controller folder?
    Last edited by JoL; Nov 29, 2005 at 15:36.

  7. #7
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What is the name of the class in mytest_controller.rb? It should be Test::MytestController.

  8. #8
    SitePoint Enthusiast
    Join Date
    May 2005
    Location
    Sweden
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, now it work just perfectly!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •