Ajax with Ruby problem


#1

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.

Name: Time:

#2

On 6/28/07, Gale CC removed_email_address@domain.invalid wrote:

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


#3

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.


#4

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 J.


#5

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 ?


#6

On Jun 27, 2007, at 10:54 PM, list. rb wrote:

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 ?


Posted via http://www.ruby-forum.com/.

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


#7

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


#8

On 6/28/07, Gale CC removed_email_address@domain.invalid wrote:

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.


#9

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-H Johansen wrote:

On 6/28/07, Gale CC removed_email_address@domain.invalid wrote:

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.


#10

John J.

On Jun 28, 2007, at 2:51 AM, Gale CC wrote:

in this case when its run from inside a javascript script. Any idea
Normally, I would have to reference the cgi-bin directory as well,


Posted via http://www.ruby-forum.com/.

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


#11

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

John J. wrote:

John J.

On Jun 28, 2007, at 2:51 AM, Gale CC wrote:

in this case when its run from inside a javascript script. Any idea
Normally, I would have to reference the cgi-bin directory as well,


Posted via http://www.ruby-forum.com/.

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


#12

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

Gale CC wrote:

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

John J. wrote:

John J.

On Jun 28, 2007, at 2:51 AM, Gale CC wrote:

in this case when its run from inside a javascript script. Any idea
Normally, I would have to reference the cgi-bin directory as well,


Posted via http://www.ruby-forum.com/.

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


#13

On Jun 28, 2007, at 8:23 PM, Gale CC wrote:

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)