SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: routes.rb

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Mar 2006
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question routes.rb

    i have searched through this forum (and the web), but havent been able to find a simple explanation of routes.rb

    i want to be able to simply load an index.rhtml when the browser loads http://localhost:3000/

    from that page i want to be able to call some controllers by using:

    Code:
    <%= link_to "Click here for buildings", :action => "buildings" %>
    to look something like http://localhost:3000/buildings

    but i simply dont understand how to without invoking another controller called index, which causes other problems.

    http://localhost:3000/index/buildings

    which tbh is fine by me, but then i get an error message telling me it doesnt exist which is true, the building controller is not under the index controller, so i dont understand again how to instruct routes.rb to do this

    any takers?

    oh and hi this is my first post

  2. #2
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Code:
    map.connect '', :controller => 'index', :action => 'buildings'
    Put this before any other map.connect rules.

  3. #3
    SitePoint Member
    Join Date
    Mar 2006
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok now http://localhost:3000/ gives me this: No action responded to buildings

    when i go to http://localhost:3000/index/ it gives me the index.rhtml file,
    but the link for buildings goes to: http://localhost:3000/
    but i need it to go to http://localhost:3000/buildings

    by changing the routes.rb to have this: map.connect 'buildings', :controller => 'index', :action => 'buildings'

    i get No action responded to buildings, but if i change it to map.connect 'buildings/list', :controller => 'index', :action => 'buildings' it works.

    however when i edit a row, and click back (using the link generated by scaffold rather that back button on browser) i get: No action responded to buildings
    Last edited by Chicane; Mar 3, 2006 at 14:16.

  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)
    Not sure why you are having a problem here...its not entirely clear what you are trying to do. You have some odd controller names (cfdbase??? Try and make your controller names clearer). What are your controllers and your actions? So far you've mentioned an IndexController (again not really a great name...its name should describe its purpose) and CfdbaseController, and a buildings action but which controller is this in?

    Out of the box, routes should do most of the things you want them to do. The only change you should have to make is the route to connect your initial controller/action (your "index" route if you like) and routes for special URL conventions.

    If you want to have a controller/action solely to act as your home/index controller, then thats fine (HomeController is a better name...it better defines its purpose than index which is just a naming convention for default web server files and in Rails' case, the default action in a controller).

    Code:
    class HomeController < ActionController::Base
      def index
      end
    end
    
    # app/views/home/index.rhtml
    Welcome to my app etc...
    
    # routes.rb
    # make sure the index.html file in the public folder is removed
    map.connect '', :controller => 'home', :action => 'index'
    
    # http://myapp/ should now load your index action on the home controller

  5. #5
    SitePoint Member
    Join Date
    Mar 2006
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok i made changes to my last post to clear up the confusion

    i have now changed index to home to create a home controller.

    what i wanted to do i to create a simple index page that i could link to other controllers such as buildings.

    so on the index.rhtml page there would be a link to buildings which would list the buildings from the database (the dbase interaction works )

    i have created a buildings controller, but when clicking on the link i get: http://localhost:3000/home/buildings

    so how do i either:

    1) get the link to remove /home/
    or
    2) put the buildings controller under /home/

  6. #6
    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 original post you said you had this in your index.rhtml file:

    Code:
    <%= link_to "Click here for buildings", :action => "buildings" %>
    Which would try and link to the buildings action in the current controller (home), hence the "/home/buildings" link. If buildings is actually a controller, what you want is:

    Code:
    <%= link_to "Click here for buildings", :controller => "buildings" %>
    Which would link to the index (default) action of your BuildingsController ("/buildings").

  7. #7
    SitePoint Member
    Join Date
    Mar 2006
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ah thanks


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
  •