# Partials in layouts?

**URL:** <https://www.ruby-forum.com/t/partials-in-layouts/175123>\
**Category:** Rails\
**Created:** [September 8, 2009, 1:27pm UTC](https://www.ruby-forum.com/t/partials-in-layouts/175123 "2009-09-08T13:27:08Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [September 8, 2009, 1:27pm UTC](https://www.ruby-forum.com/t/partials-in-layouts/175123/1 "2009-09-08T13:27:08Z")

</div>

Hi Gurus, how do I include a partial in my main layout?

In views/layouts/application.html.erb I have

`
...
<%= render :partial => 'navbar_tabs' %>
...
` No matter where I put \_navbar\_tabs.html.erb (or renamed it to \_navbar\_tabs.erb in desperation) I get the error

Showing app/views/layouts/application.html.erb where line #20 raised:

Missing template messages/\_navbar\_tabs.erb in view path app/views

Can you help?

TIA,  
Craig

---

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [September 8, 2009, 2:25pm UTC](https://www.ruby-forum.com/t/partials-in-layouts/175123/2 "2009-09-08T14:25:05Z")

</div>

Try giving the whole path in render like this:

\<%= render :partial =\> ‘/messages/navbar\_tabs’ %\> # if your partial is  
in  
messages directory.

Let us know if this does not work.

Thanks,  
Abhinav

–  
à¤…à¤­à¤¿à¤¨à¤µ  
[http://twitter.com/abhinav](http://twitter.com/abhinav)

---

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [September 8, 2009, 3:32pm UTC](https://www.ruby-forum.com/t/partials-in-layouts/175123/3 "2009-09-08T15:32:24Z")

</div>

Thanks–

\<%= render :partial =\> ‘layouts/navbar\_tabs’ %\>

works. Seems kind of ugly, though–even in layouts, shouldn’t a form  
look in its own directory for a partial?

Anyway, thanks!

---

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [September 8, 2009, 4:27pm UTC](https://www.ruby-forum.com/t/partials-in-layouts/175123/4 "2009-09-08T16:27:25Z")

</div>

Seems surprising to me as well.

## Thanks, Abhinav

à¤…à¤­à¤¿à¤¨à¤µ  
[http://twitter.com/abhinav](http://twitter.com/abhinav)

---

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [September 8, 2009, 4:57pm UTC](https://www.ruby-forum.com/t/partials-in-layouts/175123/5 "2009-09-08T16:57:52Z")

</div>

2009/9/8 Dudebot [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> No matter where I put \_navbar\_tabs.html.erb (or renamed it to  
> \_navbar\_tabs.erb in desperation) I get the error
> 
> Showing app/views/layouts/application.html.erb where line #20 raised:
> 
> Missing template messages/\_navbar\_tabs.erb in view path app/views

What happens if you name it \_navbar\_tabs.html.erb and put in in  
views/messages?

Colin

---

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [September 8, 2009, 9:24pm UTC](https://www.ruby-forum.com/t/partials-in-layouts/175123/6 "2009-09-08T21:24:13Z")

</div>

By convention, any partials that are loaded by “application.html.erb”  
are loaded by all controllers in the application. That being the  
case, when rendering app-\>messages-\>index:

The app…erb line will read "\<%= render :partial =\> ‘layouts/  
app\_wide\_partial’ %\>  
The partial will be found in views/layouts/  
\_app\_wide\_partial.html.erb  
When the line in app…erb is reached that reads “\<%= yield %\>”  
The file “views/messages/index.html.erb” is rendered

And you have a file views/layouts/application.html.erb  
that calls “\<%= render :partial =\> ‘navbar\_tabs’ %\>”  
Rails will look for the partial named: views/messages/  
\_navbar\_tabs.html.erb

It is customary practice to follow the convention “… :partial =\>  
‘directory/partial’ %\>” just so there is no confusion. Thus, if you  
had additional message related tabs, you would include in views/  
messages/index.html.erb: \<%= render :partial =\> ‘messages/  
msg\_navbar\_tabs’ %\>

---

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [September 8, 2009, 8:58pm UTC](https://www.ruby-forum.com/t/partials-in-layouts/175123/7 "2009-09-08T20:58:28Z")

</div>

On Tue, Sep 8, 2009 at 6:30 AM, Dudebot [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks–
> 
> \<%= render :partial =\> ‘layouts/navbar\_tabs’ %\>
> 
> works. Seems kind of ugly, though–even in layouts, shouldn’t a form  
> look in its own directory for a partial?

If you’re using the partial in the layout, it means the partial will be  
displayed  
on every page of the site by default. Is this the case?

-Conrad

---

<div class="post-metadata">

**Author:** ![Dudebot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@Dudebot](https://www.ruby-forum.com/u/Dudebot)\
**Post date:** [September 8, 2009, 9:32pm UTC](https://www.ruby-forum.com/t/partials-in-layouts/175123/8 "2009-09-08T21:32:24Z")

</div>

> works. Seems kind of ugly, though–even in layouts, shouldn’t a form  
> look in its own directory for a partial?

It does, you’re just misunderstanding what “its own directory” refers  
to. Your form is not a part of “layouts,” it’s a part of the  
controller rendering it.
