Ajax with Ruby problem

G

Gale CC

I'm a newbie to both Ruby and Ajax. I'm trying to use Ruby to handle the
server side of an Ajax application, but the script doesnt run and
instead of the output the source code shows up. I'm running these
scripts on Apache. I've made sure that the script is executable but its
still not running. Any help with this would be greatly appreciated. I've
tried Googling for this problem but didnt get any helpful results. My
source code looks as follows:

ruby script(test2.rb):

#!/usr/local/bin/ruby -w
response = Time.now
puts response

html script - this script is supposed to display the time in the "Time"
field when the user enters text in the "Name" field.

<html>
<body>

<script type="text/javascript">
function ajaxFunction()
{
var xmlHttp;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("Your browser does not support AJAX!");
return false;
}
}
}
xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
document.myForm.time.value=xmlHttp.responseText;
}
}
xmlHttp.open("GET","test2.rb",true);
xmlHttp.send(null);
}
</script>

<form name="myForm">
Name: <input type="text"
onkeyup="ajaxFunction();" name="username" />
Time: <input type="text" name="time" />
</form>

</body>
</html>
 
J

J-H Johansen

I'm a newbie to both Ruby and Ajax. I'm trying to use Ruby to handle the
server side of an Ajax application, but the script doesnt run and
instead of the output the source code shows up. I'm running these
scripts on Apache. I've made sure that the script is executable but its
still not running. Any help with this would be greatly appreciated. I've
tried Googling for this problem but didnt get any helpful results. My
source code looks as follows:

Try renaming the script to test2.cgi and see if that helps.
If you don't want to then you need to define .rb as a script in the
Apache config file and restart Apache. Can't recall the exact syntax
here, so try searching for cgi and see if it can be used.
ruby script(test2.rb):

response = Time.now
puts response

This line will give an error in your Apache log.
What you need to output here is some kind of header info. Not sure
what exactly needs to be present here but try:

puts "Content-type: text/html"
puts response
 
G

Gale CC

Tried both the approaches but neither worked.
I'm running these scripts from /cgi-bin directory of Apache which is set
to "execute" all the scripts, but dont know why its not working out.
 
J

John Joyce

Make sure your shebang line is correct and that your Apache knows
about Ruby.
#!/usr/local/bin/ruby

might not be it.

It maybe good to try:
#!/usr/bin/ruby

or:
#!/usr/bin/env ruby

This one checks the env to find out where ruby is located.
But both could be totally different, very easily.
Check with your Apache server admin, they will (or should) know.


John Joyce
 
G

Gale CC

I'm pretty sure the shebang line is correct. I verified by doing "which
ruby" and also ran the file independently from the shell by typing
"./test2.rb" and it works.

Also the file runs fine when its set as an "action" to a form, but not
in this case when its run from inside a javascript script. Any idea
what's wrong ?
 
J

John Joyce

Try referencing the full path to the script, i.e
xmlHttp.open("GET","/home/auser/scripts/test2.rb",true);

sounds like it is all about the Apache configuration.
If you have access, look at httpd.conf
whether you do or not, definitely check with the admin
 
J

J-H Johansen

I'm pretty sure the shebang line is correct. I verified by doing "which
ruby" and also ran the file independently from the shell by typing
"./test2.rb" and it works.

Also the file runs fine when its set as an "action" to a form, but not
in this case when its run from inside a javascript script. Any idea
what's wrong ?

Download the "web developer" plug-in for Firefox and enable while
you're developing. It's a nice feature to see each and every call you
make and you can even inspect the output from the calls.

The reference you're using here looks a bit weird though.

xmlHttp.open("GET","test2.rb",true);

Normally, I would have to reference the cgi-bin directory as well, and
this here appears to reference the Ruby script as though it was
located in the same directory as the HTML file that produces the page.
Try something like this:

xmlHttp.open("GET","/cgi-bin/test2.rb",true);

if the script is located at the root of the cgi-bin directory.
 
G

Gale CC

Damn.. I really dont believe this!!
I gave a half-hearted try to what you said and it worked!!
I was previously working inside /cgi-bin/ajax-test directory and was
thinking it should not make a difference. What you said made no sense to
me but I still gave it a try just to be sure.
Anyways, thanks so much!!

Gale
 
J

John Joyce

John Joyce


Damn.. I really dont believe this!!
I gave a half-hearted try to what you said and it worked!!
I was previously working inside /cgi-bin/ajax-test directory and was
thinking it should not make a difference. What you said made no
sense to
me but I still gave it a try just to be sure.
Anyways, thanks so much!!

Gale

So the problem was not calling the script with the /cgi-bin
directory in the path ?
And the solution was pre-pending that to the path_to_script?

Please post a clear solution, even if it is a link to a blog. this is
a most interesting problem and solution that should probably be on
the troubleshooting checklist for lots of people!
thx
 
G

Gale CC

So the problem was not calling the script with the /cgi-bin
directory in the path ?
And the solution was pre-pending that to the path_to_script?

No, I dont think just pre-pending "/cgi-bin" would do any magic. Here's
what happened from the very beginning: First of all, I've configured
Apache to "execute" anything under /cgi-bin by setting it as the
Script-Alias. And then, for making Ajax work I was testing things inside
/cgi-bin. I created a folder /ajax-test inside /cgi-bin, so my html as
well as ruby scripts lied inside /cgi-bin/ajax-test/. But the Ruby
was'nt being executed and the source code would show up instead of the
output. Now after I put the html script outside the /cgi-bin folder, it
all started working. So the solution to such a problem is keeping the
html file (or whichever file is calling the script) outside the /cgi-bin
folder, or for that matter whichever folder is set as the Script-Alias
in your Apache configuration.
Was I clear enough in explaining the problem and the solution ? Let me
know if otherwise.

Thanks all of you for all the help,
Gale
 
G

Gale CC

I'm sorry to spam the board like this, but I had to correct myself on
what I wrote before. I had a new insight which conflicts with what I
wrote before.
No, I dont think just pre-pending "/cgi-bin" would do any magic.

Yes, adding /cgi-bin does do the magic! I realized this after playing
around with Apache and html scripts for a while today. I noticed that
the httpd.conf had the line :
ScriptAlias /cgi-bin/ /usr/local/apache2/htdocs/cgi-bin/

This means that whatever that is inside
/usr/local/apache2/htdocs/cgi-bin/ would now simply be referred to as
/cgi-bin no matter where you access it from. So whatever I wrote before
is completely wrong - sorry about that.

Thanks,
Gale
 
J

John Joyce

I'm sorry to spam the board like this, but I had to correct myself on
what I wrote before. I had a new insight which conflicts with what I
wrote before.


Yes, adding /cgi-bin does do the magic! I realized this after playing
around with Apache and html scripts for a while today. I noticed that
the httpd.conf had the line :
ScriptAlias /cgi-bin/ /usr/local/apache2/htdocs/cgi-bin/

This means that whatever that is inside
/usr/local/apache2/htdocs/cgi-bin/ would now simply be referred to as
/cgi-bin no matter where you access it from. So whatever I wrote
before
is completely wrong - sorry about that.

Thanks,
Gale

Thanks for posting your solution! It may be useful to somebody else.
I thought it might be something like that. I have had my own
difficulties with Apache configuration before! (and still do from
time to time)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top