Topic: Question about partials

Sorry if this is a stupid post but can I put a menu thats in an index.html.erb into a partial so I can include it in other .html.erb files. so how would I turn

<ul>

                <li id="current"><a href="index.html">Home</a></li>

                <li><a href="style.html">Style Demo</a></li>

                <li><a href="blog.html">Blog</a></li>

                <li><a href="index.html">Services</a></li>

                <li><a href="index.html">Support</a></li>

                <li><a href="index.html">About</a></li>       

            </ul>


into a partial.

Re: Question about partials

I think you just put it into a file that begins with an underscore. I usually keep a directory in my views directory called "includes" or "partials" or I stash it under "shared". To call it you simply have to remember where you put it. Say you saved it under app/views/includes/_menu.html.erb. Then you call it from your template, say, app/views/home/index.html.erb:

 <%= render(:partial => 'includes/menu') %>

Re: Question about partials

yes put your code into a partial and you can call it up again and again where ever you like. 

Here's a form example, where I cut the similar elements out.  your menu would be similar.


#edit.html.erb

<h1>Editing post</h1>

<%= error_messages_for :post %>

<% form_for(@post) do |f| %>

  <%= render :partial => 'form', :locals => {:f =>f} %>
 

  <p>
    <%= f.submit "Update" %>
  </p>
<% end %>

<%= link_to 'Show', @post %> |
<%= link_to 'Back', posts_path %>


....
#_form.html.erb in same folder as edit.html.erb

all my common form elements


this way edit/create/new etc all use similar elements edited in only one location, from the partial.  Your _menu.html.erb partial can do something similar.

Last edited by sf2k (2009-01-06 22:20:53)