# Different cc=js files for different pages

**URL:** <https://www.ruby-forum.com/t/different-cc-js-files-for-different-pages/168199>\
**Category:** Rails\
**Created:** [May 18, 2009, 7:23am UTC](https://www.ruby-forum.com/t/different-cc-js-files-for-different-pages/168199 "2009-05-18T07:23:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![fRAnKEnSTEin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/0ea827/32.png) [@fRAnKEnSTEin](https://www.ruby-forum.com/u/fRAnKEnSTEin)\
**Post date:** [May 18, 2009, 7:23am UTC](https://www.ruby-forum.com/t/different-cc-js-files-for-different-pages/168199/1 "2009-05-18T07:23:11Z")

</div>

Hi,

I am making a new little web site. Each page has it’s own css/js files  
apart. I want to include any of that css/js files on the application  
layout depending on wich page will be loaded. For example if “upload”  
page will be loaded, the application layout should just include the  
“upload.css” and the “upload.js”.

I tryed, using an if-else aproach so, using if stataments in the app  
layout i just load the files i need, for example:

if @page == “upload”

# include upload.cc

else  
…  
end

I think this aproach is kinda ugly, so i used another one, in this  
case, i made multiple layouts for any of the pages i need to load, but  
this aproach will repeat a lot of my html code, for example: header  
and footer will be the same on each page, so making a change in any of  
these parts will require to modify all the pages layouts that i have,  
this is too much work.

Any ideas of how can i solve this? (is there any other way to do this)

Cheers,

Shirkavand

---

<div class="post-metadata">

**Author:** ![fRAnKEnSTEin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/0ea827/32.png) [@fRAnKEnSTEin](https://www.ruby-forum.com/u/fRAnKEnSTEin)\
**Post date:** [May 18, 2009, 7:51am UTC](https://www.ruby-forum.com/t/different-cc-js-files-for-different-pages/168199/2 "2009-05-18T07:51:16Z")

</div>

something like this should probably work…

in application/layout.html.erb

\<%= yield :javascripts %\>

in the individual views:

\<% content\_for :javascripts do %\>  
\<%= javascript\_include\_tag “js\_for\_page\_in\_question” %\>  
\<% end %\>

---

<div class="post-metadata">

**Author:** ![fRAnKEnSTEin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/0ea827/32.png) [@fRAnKEnSTEin](https://www.ruby-forum.com/u/fRAnKEnSTEin)\
**Post date:** [May 18, 2009, 8:01am UTC](https://www.ruby-forum.com/t/different-cc-js-files-for-different-pages/168199/3 "2009-05-18T08:01:49Z")

</div>

Hi Shirkavand,

To avoid repeating html code, you can use application layout file (/  
app/views/layouts/application.html.erb). Here you can specify any  
markup you want to be applied for all the pages in your app. If the  
layouts folder doesn’t contain layout file for some controller,  
appllication layout file will be used.  
Why do you want to use individual css and js files for each page?  
Isn’t it simpler to use one css and js files if you are developing  
little web site?

Regards,  
Bob

---

<div class="post-metadata">

**Author:** ![fRAnKEnSTEin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/0ea827/32.png) [@fRAnKEnSTEin](https://www.ruby-forum.com/u/fRAnKEnSTEin)\
**Post date:** [May 18, 2009, 8:10am UTC](https://www.ruby-forum.com/t/different-cc-js-files-for-different-pages/168199/4 "2009-05-18T08:10:36Z")

</div>

HI,

Thank you very much pharrington and bob for your fast replay.

Ok i am gonna test pharrington aproach and let you know what  
happened.

Bob: yes, you are right, this app is little so i can use just one js/  
css file. The problem is that i have developed in the past some other  
medium size sites, and they have a lot of javascript code that are  
totally different for each page, so creating a unique js file will  
make each page to load a lot of js code that will not use. So i just  
realize that i want to change this, and i wanted to start my new  
little site with this change done.

Cheers.

---

<div class="post-metadata">

**Author:** ![fRAnKEnSTEin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/0ea827/32.png) [@fRAnKEnSTEin](https://www.ruby-forum.com/u/fRAnKEnSTEin)\
**Post date:** [May 18, 2009, 11:44am UTC](https://www.ruby-forum.com/t/different-cc-js-files-for-different-pages/168199/5 "2009-05-18T11:44:40Z")

</div>

HI there,

Yes! it worked just great!! 😃

Regards
