SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    Awesome Addict
    Join Date
    Mar 2004
    Location
    Toronto, Canada
    Posts
    326
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Homepage Controller?

    I bought Agile Web Development with Rails over the weekend and I am in love. I can't believe how powerful Rails is and how much fun it makes coding! Anyway, on to my question.

    Assume I have a project here:

    C:\rails\project\

    I understand that I will find the default public view in:

    C:\rails\project\public\index.html

    I've deleted this file and have done the following in C:\rails\project\config\routes.rb:

    map.connect '', :controller => "index"

    My index controller is sitting in:

    C:\rails\project\app\controllers\index_controller.rb

    and my index.rhtml view in:

    C:\rails\project\app\views\index\index.rhtml

    Booting webrick form C:\rails\project and browsing to http://localhost:3000/ does indeed bring up my index.rhtml view. The problem is that within index.rhtml I have done the following:

    <%= link_to "Visit the store!", :action => "store" %><br />
    <%= link_to "Admin", :action => "admin" %>

    ... which point to http://localhost:3000/index/store and http://localhost:3000/index/admin rather than http://localhost:3000/store and http://localhost:3000/admin.

    My question, then, is how do I create a default index controller/view that would be used as, or act as, a typical index.php file in the web docroot of your website? Am I on the right path and just missing something?

  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)
    The default rails route will always route /controller/action to Controller::Action - if you want to route /action to SomeDefaultController::Action you'll need to setup routes.

    But, it sounds like your admin and store actions really want to be controllers of their own.

  3. #3
    Awesome Addict
    Join Date
    Mar 2004
    Location
    Toronto, Canada
    Posts
    326
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the reply Luke. I think half of my problem is the way I asked the question. What i was asking is say you have a site ... http://www.example.com. How do you integrate your controllers into your homepage (example.com/index.rhtml)? This is what I came up with:

    In config/routes.rb I changed the default route to:
    Code:
    # use the index controller as the default
    map.connect ':controller/:action/:id', :controller => 'index'
    and then in the index controller i did this:
    Code:
    <%= link_to "Visit the store!", :controller => "store", :action => "index" %><br />
    <%= link_to "Admin", :controller => "admin", :action => "index" %>
    And these links will point to something like:
    Code:
    http://www.example.com/store
    http://www.example.com/admin
    ... rather than

    Code:
    http://www.example.com/index/store
    http://www.example.com/index/admin/list

  4. #4
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well in your first example you only specified the action which is why it took the current controller (index) to be the one you wanted and linked to index/store and index/admin. In your above example you have actually specified store and admin as controllers with the action being index so they should be linking to store/index and admin/index.

    What is your "index" controller for anyway - as a rule, controller names should imply some kind of role but "index" doesn't really say much.


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
  •