SitePoint Sponsor

User Tag List

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

    running through db table to create nested list navigation

    Does anyone know of a good tutorial for doing this with ruby or ROR? I coming from a coldfusion state of mind and this is totally easy with CF. I've got a table with id, parent_id and title.

    Just need something to show me how this would be created with ruby.

    Thanks for any help!
    your brain reacts in the same way whether you are
    looking at something or thinking about it...

  2. #2
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is extremely easy with Rails First create your model:

    Code:
    ruby script/generate model Category parent_id:integer title:string
    Note that we tell Rails which attributes the model is going to have in this command: parent_id, which is an integer, and title, which is a string. Rails creates the id automatically for every model.

    So we have a migration. A migration is a script that creates the db table for you ("create_table categories ..."). You can use this script again later when deploying your app to a server, to install the table in the server's db. You can inspect the code of this migration in yourapp/db/migrate/.

    Now run this script with this command:

    Code:
    rake db:migrate
    This runs the migration Rails just created. Now we have a table in the db.

    The next step is to tell Rails that the model is a tree, so you can do things like:

    Code ruby:
    @category = Category.find(params[:id])
     
    # returns the parent of the category
    # this does "SELECT * FROM `categories` where id=#{@category.parent_id}"
    @category.parent
     
    # returns all children of this category
    # "SELECT * FROM `categories` where parent_id=#{@category.id}"
    @category.children

    To add these methods to the model we add acts_as_tree to the model class:

    Code:
    class Category < ActiveRecord::Base
      acts_as_tree
    end
    acts_as_tree knows about the parent_id column, because it's a convention in Rails.

    So it's just 3 steps:

    1. run: "ruby script/generate model Category parent_id:integer title:string"
    2. run: "rake db:migrate"
    3. add the line "acts_as_tree" to your model

  3. #3
    SitePoint Guru downtroden's Avatar
    Join Date
    Dec 2004
    Location
    illinois
    Posts
    978
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    wow... that's pretty cool.

    The acts_as_tree tutorial seemed a lot more intense.

    So would the view be something like:

    Code Ruby:
    <% if parent_id = "" %>
       <%= @category.parent %>
    <% else %>
       <%= @category.children %>
    <% end %>

    Or does the act_as_tree method know to make a nested list with unordered list tags?
    your brain reacts in the same way whether you are
    looking at something or thinking about it...

  4. #4
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't think it does. You can create a helper for this:

    Code ruby:
    def tree_to_ul(object, on=&:children, &block)
      yield(object) +
      content_tag(:ul,
        on.call(object).map{|x| content_tag(:li, tree_to_ul(x,on,&block))}.join)
    end

    This is how it's *supposed* to work:

    Code:
    <&#37;= tree_to_ul(@category) do |category| %>
      <%= link_to category.title, category_url(category) %>
    <% end %>
    This creates the nested ul's and li's, with links created by the block.


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
  •