SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    May 2007
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Persistent navigation

    I'd like to set up some persistent navigation at the top of my web pages and a good place to do this seems to be in application.rhtml. The sitepoint ruby book demonstrates something along the lines of this:

    Code:
      <ul id="navigation">
         <li><%= link_to 'Front page stories', 
    	    :controller => 'story' </li>
          <li><%= link_to 'Submit a new story', 
    	    :controller => 'story', :action => 'new'%></li> 
      </ul>
    I'm running into problems because I'd like to have my navigation link to some dynamically created topic pages. That is, I've got a model called Topic that can have many stories (a story belongs to a topic). In my example, I've got two topics, "sports" and "art", and I want my navigation to have links to both of them. I need some way to identify the various topics and was thinking of using a loop such as

    <% for topic in @topics %>

    My few attempts at working this out haven't worked. Do I need to create an instance variable in application.rb called @topics? How do I access it? Is there a better way to do this?
    Thanks!

  2. #2
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can create it using a before filter like this:

    Code Ruby:
    # in application.rb
     
      before_filter :initialize_topics
     
      def initialize_topics
         @topics = ...
      end

    But you probably want to cache this if you're going to deploy the application.


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
  •