Forum: Ruby on Rails Javascript blues

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B92d6cc66a88aaba5482f9284ecf2ded?d=identicon&s=25 Tharfagreinir (Guest)
on 2007-02-02 13:55
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:

<script src="/javascripts/user_login.js?[number]"
type="text/javascript"></script>

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:

<form action="/user/signup" method="post" onsubmit="return
user_signup();">

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?
3a0f2ec009ceffdd65f056540a409a23?d=identicon&s=25 Chris Mear (Guest)
on 2007-02-02 14:04
(Received via mailing list)
On Feb 2, 12:55 pm, Tharfagreinir <rails-mailing-l...@andreas-s.net>
wrote:
>
> 4) 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
83ca41657a99b65d99889abe712ba5e2?d=identicon&s=25 Jason Roelofs (Guest)
on 2007-02-02 14:25
(Received via mailing list)
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
B92d6cc66a88aaba5482f9284ecf2ded?d=identicon&s=25 Tharfagreinir (Guest)
on 2007-02-02 14:32
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.
83ca41657a99b65d99889abe712ba5e2?d=identicon&s=25 Jason Roelofs (Guest)
on 2007-02-02 14:58
(Received via mailing list)
Is the file in #{RAILS_ROOT}/public/javascripts ?

What version of Rails / Ruby are you using?

Jason
B92d6cc66a88aaba5482f9284ecf2ded?d=identicon&s=25 Tharfagreinir (Guest)
on 2007-02-02 15:06
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
3a0f2ec009ceffdd65f056540a409a23?d=identicon&s=25 Chris Mear (Guest)
on 2007-02-02 16:09
(Received via mailing list)
On Feb 2, 2:06 pm, Tharfagreinir <rails-mailing-l...@andreas-s.net>
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
B92d6cc66a88aaba5482f9284ecf2ded?d=identicon&s=25 Tharfagreinir (Guest)
on 2007-02-02 16:26
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!
This topic is locked and can not be replied to.