SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Guru downtroden's Avatar
    Join Date
    Dec 2004
    Location
    illinois
    Posts
    974
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    navigation system from database

    i've got a navigation system coming from a database, table is
    id, name, parent_id

    menu is working fine, the only problem i'm having now is how to get the controller and action name into the link_to function.

    would i just create two columns, controller and action?
    your brain reacts in the same way whether you are
    looking at something or thinking about it...

  2. #2
    SitePoint Guru downtroden's Avatar
    Join Date
    Dec 2004
    Location
    illinois
    Posts
    974
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    really? no opinions?
    your brain reacts in the same way whether you are
    looking at something or thinking about it...

  3. #3
    l 0 l silver trophybronze trophy lo0ol's Avatar
    Join Date
    Aug 2002
    Location
    Palo Alto
    Posts
    5,329
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Wouldn't mind learning more about the problem. parent_id links to what, and the parent itself is a type of what? Why do you need controller and action names? It's a bit ambiguous given what you've written.

  4. #4
    SitePoint Guru downtroden's Avatar
    Join Date
    Dec 2004
    Location
    illinois
    Posts
    974
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i'm creating a navigation system using unordered lists with son of suckerfish CSS method.

    parent_id would be the parent category, so

    Code:
    id     name       parent_id
    1      animal     NULL
    2      machine    NULL
    3      car        2
    4      dog        1
    that would give me the following unordered list
    Code:
    <ul>
      <li>animal
        <ul>
          <li>dog</li>
        </ul>
    </li>
    <li>machine
        <ul>
          <li>car</li>
        </ul>
    </li>
    </ul>
    my problem is getting the machine or animal link to go to the index method of the animal or machine controller or the car link to go to the car method of the machine controller.
    your brain reacts in the same way whether you are
    looking at something or thinking about it...

  5. #5
    l 0 l silver trophybronze trophy lo0ol's Avatar
    Join Date
    Aug 2002
    Location
    Palo Alto
    Posts
    5,329
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    There's likely a few ways to do this... one place to start is to make a few helper methods:
    Code Ruby:
    # helper methods
    def parent_link(object)
      object.parent ? object.parent.name : object.name
    end
     
    def parent_action(object)
      object.parent ? object.name : 'index'
    end
     
    # in your view, assuming you have a navigation_item
    link_to navigation_item, :controller => parent_controller(navigation_item), :action => parent_action(navigation_item)

    You can also sort of vamp on that idea and move that logic into your model, which could make sense and might be easier to test, but it also depends on if you change these methods to be more view-oriented with additional link properties (in which case, helpers might be the better decision).


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
  •