# Sharing ruby code in view layout files

**URL:** <https://www.ruby-forum.com/t/sharing-ruby-code-in-view-layout-files/143281>\
**Category:** Rails\
**Created:** [July 16, 2008, 11:47pm UTC](https://www.ruby-forum.com/t/sharing-ruby-code-in-view-layout-files/143281 "2008-07-16T23:47:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![amrita](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dc4da7/32.png) [@amrita](https://www.ruby-forum.com/u/amrita)\
**Post date:** [July 16, 2008, 11:47pm UTC](https://www.ruby-forum.com/t/sharing-ruby-code-in-view-layout-files/143281/1 "2008-07-16T23:47:48Z")

</div>

Hi,

I have a block of code that is used by the layout file (html.erb) of  
every view. What is the best way to include this piece of code in all  
layout files?

Can I actually include a Ruby file in an html.erb file, analogous to  
how files are included in PHP?

Thanks,  
Amrita

---

<div class="post-metadata">

**Author:** ![amrita](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dc4da7/32.png) [@amrita](https://www.ruby-forum.com/u/amrita)\
**Post date:** [July 16, 2008, 11:54pm UTC](https://www.ruby-forum.com/t/sharing-ruby-code-in-view-layout-files/143281/2 "2008-07-16T23:54:51Z")

</div>

There is usually only one layout file. What are you trying to do? Views  
can  
share code by using helpers. You put them in application\_helper.rb.

> I have a block of code that is used by the layout file (html.erb) of  
> every view. What is the best way to include this piece of code in all  
> layout files?
> 
> Can I actually include a Ruby file in an html.erb file, analogous to  
> how files are included in PHP?

There is no need to do a “include”, helper methods in  
application\_helper.rb  
are automatically made available to all the views.

–  
[http://www.rubyplus.org/](http://www.rubyplus.org/)  
Free Ruby and Rails Screencasts

---

<div class="post-metadata">

**Author:** ![amrita](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dc4da7/32.png) [@amrita](https://www.ruby-forum.com/u/amrita)\
**Post date:** [July 17, 2008, 12:14am UTC](https://www.ruby-forum.com/t/sharing-ruby-code-in-view-layout-files/143281/3 "2008-07-17T00:14:09Z")

</div>

On Wed, 2008-07-16 at 14:36 -0700, amrita wrote:

> Hi,
> 
> I have a block of code that is used by the layout file (html.erb) of  
> every view. What is the best way to include this piece of code in all  
> layout files?
> 
> Can I actually include a Ruby file in an html.erb file, analogous to  
> how files are included in PHP?

* * *

I don’t know that this is a ‘best’ way but I have app/view/shared and  
then in the appropriate area in my layout, I have…

\<%= render :partial =\> “shared/name\_of\_shared\_code” %\>

Craig

---

<div class="post-metadata">

**Author:** ![amrita](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dc4da7/32.png) [@amrita](https://www.ruby-forum.com/u/amrita)\
**Post date:** [July 17, 2008, 12:25am UTC](https://www.ruby-forum.com/t/sharing-ruby-code-in-view-layout-files/143281/4 "2008-07-17T00:25:16Z")

</div>

Thanks! I gathered something was wrong with the way my application was  
built. So, I shifted to a single layout file and specified the layout  
in the application controller.

But its always good to know how code can be shared.

Thanks, Craig and Bala.
