# Javascript\_include within single views

**URL:** <https://www.ruby-forum.com/t/javascript-include-within-single-views/155466>\
**Category:** Rails\
**Created:** [December 12, 2008, 3:40am UTC](https://www.ruby-forum.com/t/javascript-include-within-single-views/155466 "2008-12-12T03:40:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [December 12, 2008, 3:40am UTC](https://www.ruby-forum.com/t/javascript-include-within-single-views/155466/1 "2008-12-12T03:40:44Z")

</div>

Are you allowed to use the javascript\_include\_tag to include  
javascript files for specific views? I have included the default  
javascript files in the element of my site layout file, but  
when I try and include a javascript file for a specific view within a  
new element in that view it does not work. I guess my question  
is: is it okay to create separate elements for specific views?  
Will all the code get rendered that is within these new   
elements and the elements in my layout file? or will there be  
a conflict?

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [December 12, 2008, 3:55am UTC](https://www.ruby-forum.com/t/javascript-include-within-single-views/155466/2 "2008-12-12T03:55:25Z")

</div>

On Thu, Dec 11, 2008 at 6:40 PM, David [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Are you allowed to use the javascript\_include\_tag to include  
> javascript files for specific views? I have included the default  
> javascript files in the element of my site layout file, but  
> when I try and include a javascript file for a specific view within a  
> new element in that view it does not work. I guess my question  
> is: is it okay to create separate elements for specific views?  
> Will all the code get rendered that is within these new   
> elements and the elements in my layout file? or will there be  
> a conflict?

Yes, you are allowed to use the javascript\_include\_tag to include  
javascript  
files for specific view templates. Can you post your code so that  
people  
can better assist you because I don’t follow your question.

-Conrad

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [December 12, 2008, 8:51am UTC](https://www.ruby-forum.com/t/javascript-include-within-single-views/155466/3 "2008-12-12T08:51:35Z")

</div>

so would the following be redundant then?

\<% content\_for :javascript do %\>  
\<%= javascript\_tag “new javascript function” %\>  
\<% end %\>

assuming i have \<%= yield :javascript %\>

in my main layout file.

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [December 12, 2008, 10:12am UTC](https://www.ruby-forum.com/t/javascript-include-within-single-views/155466/4 "2008-12-12T10:12:35Z")

</div>

David wrote:

> so would the following be redundant then?
> 
> \<% content\_for :javascript do %\>  
> \<%= javascript\_tag “new javascript function” %\>  
> \<% end %\>
> 
> assuming i have \<%= yield :javascript %\>
> 
> in my main layout file.

Please study this tutorail

> **[\#8 Layouts and content\_for - RailsCasts](http://railscasts.com/episodes/8-layouts-and-content-for)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.
