# Javascript blues

**URL:** <https://www.ruby-forum.com/t/javascript-blues/84218>\
**Category:** Rails\
**Created:** [February 2, 2007, 1:55pm UTC](https://www.ruby-forum.com/t/javascript-blues/84218 "2007-02-02T13:55:06Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tharfagreinir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8baadc/32.png) [@Tharfagreinir](https://www.ruby-forum.com/u/Tharfagreinir)\
**Post date:** [February 2, 2007, 1:55pm UTC](https://www.ruby-forum.com/t/javascript-blues/84218/1 "2007-02-02T13:55:06Z")

</div>

Hi,

I’m just getting started playing around with Ruby. So far I’ve been able  
to resolve all issues that have popped up on my own, but this particular  
one has been giving me headaches for way too long now.

The problem is that I can’t seem to be able to include javascript files  
into my pages. I’m using the javascript\_include\_tag to do this, and I’ve  
verified that the generated path is correct and points to the file that  
I want to include.

Let’s do an example:

A line in the generated html code looks like this:

In /public/javascript I have a file called user\_login.js that has a  
function called user\_signup. Then, later in the html file, I have  
something like this:

However, when I submit the form, the user\_signup function is apparently  
undefined. I have verified through various means that the problem indeed  
seems to be that the file is not included, e.g. if I include an  
alert(‘OK’) on the top of my js file, this does not get called.

Now, some details. I am using the fast-CGI dispatcher via a rewrite  
condition in .htaccess. However, I have not found anything anywhere  
saying that you need to preclude the javascripts path there, and what’s  
more, when I try do so with something like

RewriteCond %{REQUEST\_URI} ^/javascripts/(.\*)$ [OR]

… it fails; a request for /javascripts/whatever still goes to the  
dispatcher.

Leaving this aside, I started thinking about routing - maybe RoR is  
supposed to route the request for the js file, but this is failing  
somehow? However, I have found no information about this. I just have  
some default routing set up in my route.rb file:

map.connect ‘:controller/:action/:id’

So my questions are:

1. Is it necessary to preclude the /javascripts/ path from going to the  
dispatcher in order to be able to include the files there?

2. If so, what am I doing wrong? Why is RewriteCond %{REQUEST\_URI}  
^/javascripts/(.\*)$ [OR] not working?

3. If not, am I supposed to set up some routing for this to work?

4. If I’m not supposed to set up any .htaccess precludes nor routing,  
what could possibly be going wrong?

---

<div class="post-metadata">

**Author:** ![Tharfagreinir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8baadc/32.png) [@Tharfagreinir](https://www.ruby-forum.com/u/Tharfagreinir)\
**Post date:** [February 2, 2007, 2:04pm UTC](https://www.ruby-forum.com/t/javascript-blues/84218/2 "2007-02-02T14:04:27Z")

</div>

On Feb 2, 12:55 pm, Tharfagreinir [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 1. If I’m not supposed to set up any .htaccess precludes nor routing,  
> what could possibly be going wrong?

You shouldn’t need to change any of the default settings to get this  
to work.

What do you get if you just type  
[http://localhost:3000/javascripts/user\_login.js](http://localhost:3000/javascripts/user_login.js)  
into your browser address bar? Does it display/download the file okay,  
or do you get an error?

Chris

---

<div class="post-metadata">

**Author:** ![Tharfagreinir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8baadc/32.png) [@Tharfagreinir](https://www.ruby-forum.com/u/Tharfagreinir)\
**Post date:** [February 2, 2007, 2:25pm UTC](https://www.ruby-forum.com/t/javascript-blues/84218/3 "2007-02-02T14:25:29Z")

</div>

Also, please go pick up the FireBug extension for Firefox (you are using  
Firefox, right?): [https://addons.mozilla.org/firefox/1843/](https://addons.mozilla.org/firefox/1843/)

This will let you know if there’s a compilation / running error with  
your JS  
file.

Jason

---

<div class="post-metadata">

**Author:** ![Tharfagreinir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8baadc/32.png) [@Tharfagreinir](https://www.ruby-forum.com/u/Tharfagreinir)\
**Post date:** [February 2, 2007, 2:58pm UTC](https://www.ruby-forum.com/t/javascript-blues/84218/4 "2007-02-02T14:58:32Z")

</div>

Is the file in #{RAILS\_ROOT}/public/javascripts ?

What version of Rails / Ruby are you using?

Jason

---

<div class="post-metadata">

**Author:** ![Tharfagreinir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8baadc/32.png) [@Tharfagreinir](https://www.ruby-forum.com/u/Tharfagreinir)\
**Post date:** [February 2, 2007, 3:06pm UTC](https://www.ruby-forum.com/t/javascript-blues/84218/5 "2007-02-02T15:06:43Z")

</div>

Yes, the file is definitely there. I’m very confident that this is not  
the issue. Interestingly, I also get a routing error on files in  
#{RAILS\_ROOT}/public, such as robots.txt. Is this normal?

My versions:

Ruby: 1.8.5  
Rails: 1.1.6

---

<div class="post-metadata">

**Author:** ![Tharfagreinir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8baadc/32.png) [@Tharfagreinir](https://www.ruby-forum.com/u/Tharfagreinir)\
**Post date:** [February 2, 2007, 2:32pm UTC](https://www.ruby-forum.com/t/javascript-blues/84218/6 "2007-02-02T14:32:31Z")

</div>

Good to know what I should be focusing on - I’ll forget about .htaccess  
then.

When I try accessing the js file via the browser I get:

* * *

Routing Error

Recognition failed for “/javascripts/user\_login.js”

* * *

… so it truly seems to be a case of something wrong with the routing  
(duh).

Thanks for the FireBug tip - of course I’m using Firefox 🙂 Open source  
all the way.

---

<div class="post-metadata">

**Author:** ![Tharfagreinir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8baadc/32.png) [@Tharfagreinir](https://www.ruby-forum.com/u/Tharfagreinir)\
**Post date:** [February 2, 2007, 4:09pm UTC](https://www.ruby-forum.com/t/javascript-blues/84218/7 "2007-02-02T16:09:44Z")

</div>

On Feb 2, 2:06 pm, Tharfagreinir [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Yes, the file is definitely there. I’m very confident that this is not  
> the issue. Interestingly, I also get a routing error on files in  
> #{RAILS\_ROOT}/public, such as robots.txt. Is this normal?

No, that’s not normal. Don’t suppose you’ve put some funny custom  
routes in your routes.rb file?

To figure out whether this problem is specific to this app, or whether  
it’s a general problem with your system, try creating a new empty  
Rails app, dump a javascript file in public/javascripts, and see if  
your browser can load it.

Chris

---

<div class="post-metadata">

**Author:** ![Tharfagreinir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8baadc/32.png) [@Tharfagreinir](https://www.ruby-forum.com/u/Tharfagreinir)\
**Post date:** [February 2, 2007, 4:26pm UTC](https://www.ruby-forum.com/t/javascript-blues/84218/8 "2007-02-02T16:26:47Z")

</div>

I tried making a fresh application, and that worked better.

Looking at the files that were generated, I noticed that the .htaccess  
file was substantially meatier than the one I had in the other app. It  
seems that I was careless in that I downloaded a sample .htaccess file  
from somewhere and replaced the old one. On restoring this file from the  
new app, I was able to get things working correctly again.

Thanks a lot for your assistance in tracking this down, guys!
