Javascript blues

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?

On Feb 2, 12:55 pm, Tharfagreinir [email protected]
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
into your browser address bar? Does it display/download the file okay,
or do you get an error?

Chris

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

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

Jason

Is the file in #{RAILS_ROOT}/public/javascripts ?

What version of Rails / Ruby are you using?

Jason

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

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 :slight_smile: Open source
all the way.

On Feb 2, 2:06 pm, Tharfagreinir [email protected]
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

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!