AJAX without Rails

Hi, I'm pretty new to Ajax.

I need to do some simple web interfaces for some project I have. A
friend told me Rails was a bit to much for what I need, so I'm using
eruby and markaby, and I'm pretty happy, and things are kept pretty
sweet and simple for now.

However I would like to use a little of Ajax, what is the best way to go
about it? How do Ajax work for ruby? seems like rails has support for
Ajax that makes it simple. Can i have the same for my eruby scripts
without having to install any MVC system like Rails or anything?

I would basically would like to use Ajax to send parts of the page to be
process on the server and use the results to modify how this parts are
shown. For example, send a paragraph from the page to perform named
entity extraction, and then use the results to highlight these named
entities.

Thanks for your time.

- mikisvaz

···

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

Actually, I'm not sure I'm using eruby, I'm using mod_ruby and
generating the html with markaby. I'm a bit lost with all the technology
:p.

···

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

AJAX is fairly simple, at least as long as you ignore the "XML" part
of it and use HTML or JSON (Javascript Over Network) to deal with it.

The very easiest way to do AJAX is to just have a new command
handler/new script that output the HTML you want to replace with, and
do something like this (example with the jQuery library):

<!-- Include jQuery library, replace with your own copy -->
<script type="text/javascript"
src="/publish/files/jquery-nightly-20070801.js"></script>

<!-- Add a method to jQuery for pure replace; this can also be done
other ways, I just included this because this is how my code was
structured already -->
<script type="text/javascript">
jQuery.fn.replace = function() {
  var stack = ;
  return this.domManip(arguments, true, 1, function(a){
    this.parentNode.replaceChild( a, this );
    stack.push(a);
  }).pushStack( stack );
};
</script>

<!-- Actual AJAX (actually, AJAH - Asynchronous Javascript And HTML) -->
<!-- Notice: the ready setup here is to call this stuff when the page
DOM is ready.
     What happens here is that the POST equivalent of
approve.wa2?_cmd=close_edition;EditionId=2747 gets called,
     outputs some HTML, and the #editioncloser input element shown
below gets replaced with
     whatever that HTML describes. You can trivially replace what
script gets called, what args it takes, and which element it replace.
-->
<script type="text/javascript">
<!--
$(document).ready(function(){
  $("#editioncloser").click(function(){
    $.post("approve.wa2", { EditionId: 2474, _cmd: "close_edition" },
    function(data) {
  $("#editioncloser").replace(data);
    });
    return false;
  });
});
-->

</script>

<!-- HTML code for the input element that I will be replacing -->
  <input type="submit" value=" Lukk utgave " id="editioncloser">

That's simple AJAX.

If you want to include JSON data and do DOM manipulation directly, you
can do JSON calls like this:

    $.getJSON("/autodb/ajaxapi.wa2",{_cmd: cmd, xBoatType: xBoatType,
xBrand: xBrand, xModel: xModel}, function(j) {
        var elm = document.forms['search'].elements[id];
        RemoveChildren(elm);
        for (var i=0; i<j.length; i++) {
            AddOption(j[i].optValue, j[i].optDisplay, j[i].optSelected, elm);
        }
    })

Here, you need to have your callback method output "JSON", JavaScript
structures outputted over net. You can find examples of it on the
net; for a simple example, here's something that the above script
("ajaxapi.wa2") outputs for one of our structures:
[{optSelected: 0, optValue: 0, optDisplay: 'Alle (80)'},{optSelected:
0, optValue: 8001005, optDisplay: 'Fiskebåt/Sjark (2)'},{optSelected:
0, optValue: 8001008, optDisplay: 'Gummibåt (0)'},{optSelected: 0,
optValue: 8001006, optDisplay: 'Jolle/Åpen båt (4)'},{optSelected: 0,
optValue: 8001010, optDisplay: 'Kano/Kajakk (0)'},{optSelected: 0,
optValue: 8001001, optDisplay: 'Motorbåt (64)'},{optSelected: 0,
optValue: 8001003, optDisplay: 'Motorseiler (0)'},{optSelected: 0,
optValue: 8001009, optDisplay: 'RIB (0)'},{optSelected: 0, optValue:
8001012, optDisplay: 'Seilbrett, (0)'},{optSelected: 0, optValue:
8001002, optDisplay: 'Seilbåt (8)'},{optSelected: 0, optValue:
8001007, optDisplay: 'Skjærgårdsjeep (0)'},{optSelected: 0, optValue:
8001004, optDisplay: 'Snekke (2)'},{optSelected: 0, optValue: 8001011,
optDisplay: 'Vannscooter (0)'}]

Notice how close it is to Ruby array/hash structure? The only thing
is that hash keys must be followed by : instead of =>. Remember to
escape your single-quotes; they almost certainly WILL show up even if
you're sure they won't. (Or at least they did for us...)

Hope that helped a bit.

Eivind.

···

On Nov 20, 2007 5:34 PM, Miki Vz <mikisvaz@yahoo.com> wrote:

Hi, I'm pretty new to Ajax.

I need to do some simple web interfaces for some project I have. A
friend told me Rails was a bit to much for what I need, so I'm using
eruby and markaby, and I'm pretty happy, and things are kept pretty
sweet and simple for now.

However I would like to use a little of Ajax, what is the best way to go
about it? How do Ajax work for ruby? seems like rails has support for
Ajax that makes it simple. Can i have the same for my eruby scripts
without having to install any MVC system like Rails or anything?

Miki Vz <mikisvaz@yahoo.com> wrote: Actually, I'm not sure I'm using eruby, I'm using mod_ruby and
generating the html with markaby. I'm a bit lost with all the technology
:p.

···

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

Hallå Eivind.
E du svensk månne?
Har svårt o få tag på någon som faktskt kan förklara saker o ting...
/Nico

Isn't this precisely a rails tutorial? I'm trying not to use rails,
since I really have no database or anything.

Deepak Vohra wrote:

···

Ajax on Rails • The Register

Miki Vz <mikisvaz@yahoo.com> wrote: Actually, I'm not sure I'm using
eruby, I'm using mod_ruby and
generating the html with markaby. I'm a bit lost with all the technology
:p.

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

The Ajax support in Rails is definitely more fully-developed than in
any other Ruby framework, but any Ruby framework can be used to
develop Ajax, and Rails' Ajax support is something of a mixed
blessing. Although it's in some respects very powerful, I've often had
to resort to feeding it BS commands just so it would generate its
code, which I would then copy, paste, and edit. It enables a lot of
stuff that seemed really cool in 2006 automatically, but any time you
need to do something even slightly different from what comes in the
box, you have to go to JavaScript to work with it manually. So Rails'
Ajax support is always *useful*, but it's really only *powerful* if
you understand how it works and have the ability to actually write
JavaScript when you need to.

I don't know how the Ajax support in other Ruby frameworks stacks up,
but I do know you have equivalent shortcuts in Seaside, and I did
attempt unsuccessfully to use Rails' Ajax shortcuts outside of Rails
the other day. In theory it should be really simple, but apparently
the theory must have been wrong, because in practice it was involved.

The Ajax support is mainly a set of JavaScript helper methods which
live in a library called ActionView, but I had to require its
companion library ActionController as well, and first - it has to come
first - in order for ActionView to properly load, or initialize, or
something. (Since both libraries live in a gem called actionpack, I
thought I could just require actionpack, but that doesn't work.) Once
you have both libraries required, and in the proper order, you can now
get to the JavaScript helper methods, but you still can't actually use
them. They implode due to the lack of a content_tag method. There's a
content_tag method within ActionView, so you can get to it, but I
didn't bother - I think probably you have to go through some more
steps to mimic Rails' loading process, and it seemed like an awful lot
of work.

Most of Rails' JavaScript helpers don't actually have any deep ties to
Rails, and would be useful for other frameworks as well, so it's a
pity this isn't easier to do. I do know where to look, probably - the
jRails plugin allows you to swap out Prototype and Scriptaculous with
jQuery instead, so the code for that is probably a good place to
start.

···

--
Giles Bowkett

Podcast: http://hollywoodgrit.blogspot.com
Blog: http://gilesbowkett.blogspot.com
Portfolio: http://www.gilesgoatboy.org
Tumblelog: http://giles.tumblr.com

Ruby on Rails is the only Ajax framework for Ruby.
  http://ajaxpatterns.org/Ruby_Ajax_Frameworks

  Isn't this precisely a rails tutorial? I'm trying not to use rails,
since I really have no database or anything.

Deepak Vohra wrote:

···

Miki Vz <mikisvaz@yahoo.com> wrote:

Ajax on Rails • The Register

Miki Vz wrote: Actually, I'm not sure I'm using
eruby, I'm using mod_ruby and
generating the html with markaby. I'm a bit lost with all the technology
:p.

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

I ended up using Camping and calling the Ajax by hand. I din't know how
good the Ajax libraries where, prototype, etc. It's easy enogh actually.
I still have trouble doing some things, but I hope to figure them out as
soon as I have a little time.

Cheers

-Miki

···

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

:'(. Ok then, thanx.

Deepak Vohra wrote:

···

Ruby on Rails is the only Ajax framework for Ruby.
  http://ajaxpatterns.org/Ruby_Ajax_Frameworks

Miki Vz <mikisvaz@yahoo.com> wrote:
  Isn't this precisely a rails tutorial? I'm trying not to use rails,
since I really have no database or anything.

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

Deepak Vohra wrote:

Ruby on Rails is the only Ajax framework for Ruby.
  http://ajaxpatterns.org/Ruby_Ajax_Frameworks

I don't think that's quite true - doesn't Nitro have AJAX support?

···

--
Alex

Miki Vz <mikisvaz@yahoo.com> wrote:
  Isn't this precisely a rails tutorial? I'm trying not to use rails, since I really have no database or anything.

Deepak Vohra wrote:

Ajax on Rails • The Register

Miki Vz wrote: Actually, I'm not sure I'm using eruby, I'm using mod_ruby and
generating the html with markaby. I'm a bit lost with all the technology
:p.

AJAX is simply what the name implies:
Asynchronous Javascript And Xml
It means using Javascript for DOM scripting (changing the structure of the document or web page) and using Javascript to send xml requests to a remote location (the server the site comes from)
That's it.
You can implement AJAX with or without Rails.
With or without Ruby.
Ruby and AJAX are not aware of each other.
Ruby is on the server responding to requests.
Javascript and html are sent to the client.
Javascript can send requests to the server.

You can totally do this without Rails or Nitro or whatever.
It is about serving pages with Javascript in them.
Check out Scriptaculous, Prototype, Mochikit, etc...

···

On Nov 20, 2007, at 12:14 PM, Alex Young wrote:

Deepak Vohra wrote:

Ruby on Rails is the only Ajax framework for Ruby.
  http://ajaxpatterns.org/Ruby_Ajax_Frameworks

I don't think that's quite true - doesn't Nitro have AJAX support?

--
Alex

Miki Vz <mikisvaz@yahoo.com> wrote:
  Isn't this precisely a rails tutorial? I'm trying not to use rails, since I really have no database or anything.
Deepak Vohra wrote:

Ajax on Rails • The Register

Miki Vz wrote: Actually, I'm not sure I'm using eruby, I'm using mod_ruby and
generating the html with markaby. I'm a bit lost with all the technology
:p.

Miki Vz schrieb:

:'(. Ok then, thanx.

Keep smiling :slight_smile:

AJAX can be so simple with just Mongrel (no Rails required at all!!!!!):

require 'rubygems'
require 'mongrel'
require 'json'

class AjaxHandler < Mongrel::HttpHandler
   def process(req, res)
     res.start(200) do |header, out|
       out << JSON.dump({"Hello" => "AJAX!"})
     end
   end
end

Mongrel::Configurator.new :host => '127.0.0.1' do
   listener :port => 5000 do
     uri '/', :handler => AjaxHandler.new
     run
     join
   end
end

All you need are the 'json' and 'mongrel' gems. In your HTML page simply use jquery (www.jquery.com), and make a AJAX call to the server. That's so easy!

Regards,

   Michael

Alex Young wrote:

Deepak Vohra wrote:

Ruby on Rails is the only Ajax framework for Ruby.

That's a laughable claim.

  http://ajaxpatterns.org/Ruby_Ajax_Frameworks

I don't think that's quite true - doesn't Nitro have AJAX support?

Yes.

And it's easy enough to do in Merb, Camping, Ramaze, et al if they do not also have it baked in.

···

--
James Britt

"Every object obscures another object."
    - Luis Bunuel

John Joyce wrote:

AJAX is simply what the name implies:
Asynchronous Javascript And Xml
It means using Javascript for DOM scripting (changing the structure of the document or web page) and using Javascript to send xml requests to a remote location (the server the site comes from)
That's it.
You can implement AJAX with or without Rails.
With or without Ruby.
Ruby and AJAX are not aware of each other.
Ruby is on the server responding to requests.
Javascript and html are sent to the client.
Javascript can send requests to the server.

You can totally do this without Rails or Nitro or whatever.
It is about serving pages with Javascript in them.
Check out Scriptaculous, Prototype, Mochikit, etc...

This is true; however I understood the question to be asking whether there were any Ruby frameworks that specifically help with the server-side part of the equation. Rails' integration with Scriptaculous and Prototype is one example, but the OP mentioned that wasn't appropriate. I was attempting to provide an alternative - I understand that Nitro's AJAX support is comparable.

···

--
Alex

On Nov 20, 2007, at 12:14 PM, Alex Young wrote:

Deepak Vohra wrote:

Ruby on Rails is the only Ajax framework for Ruby.
  http://ajaxpatterns.org/Ruby_Ajax_Frameworks

I don't think that's quite true - doesn't Nitro have AJAX support?

--
Alex

Miki Vz <mikisvaz@yahoo.com> wrote:
  Isn't this precisely a rails tutorial? I'm trying not to use rails, since I really have no database or anything.
Deepak Vohra wrote:

Ajax on Rails • The Register

Miki Vz wrote: Actually, I'm not sure I'm using eruby, I'm using mod_ruby and
generating the html with markaby. I'm a bit lost with all the technology
:p.

Thanks a lot for your replies guys, I have a lot of information here to
digest but I think I know what to look for.

Miki

···

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

That expression always makes me think of this:

  http://xkcd.com/123/

···

On 22 Nov 2007, at 00:00, James Britt wrote:

Alex Young wrote:

Deepak Vohra wrote:

Ruby on Rails is the only Ajax framework for Ruby.

That's a laughable claim.

Benjohn Barnes wrote:

Alex Young wrote:

Deepak Vohra wrote:

Ruby on Rails is the only Ajax framework for Ruby.

That's a laughable claim.

That expression always makes me think of this:

  xkcd: Centrifugal Force

I just ran across this post. I am taking swimming lessons in Ruby,
Rails, AJAX, and the like. I was looking for some help on how to take
advantage of AJAX using Ruby. I'm wanting to start off simple with a
call to a cgi script which will pass data back to the page updating a
<div>. I found Michael Neumann's post of interest as I have zero Java
or Javascript experience. I'm an old salt at VB & Delphi coding Database
apps. I googled json as I didn't know what that was and also JQuery.
So I think this all sounds pretty cool. One question I have. Looking
at the JQuery info, it seems that all the page magic is coded into the
web page itself. I didn't see any info (perhaps that's not how JQuery
works) on accessing the DOM elements from a cgi or ruby code. My need
is to process one or more files on a syslog server for information and
pass the info back to the browser. I could use some clarification on
what I need to be looking for to accomplish this. Thanks so much for
any feedback you may have.

tonyd

···

On 22 Nov 2007, at 00:00, James Britt wrote:

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

That's not how it works, period. JavaScript running in the browser
sends a request to a server process, which returns a response; it's
up to the in-page JS to handle the response appropriately.

But there's no way for the server to directly manipulate anything on
the client.

···

On Tue, Mar 18, 2008 at 1:51 PM, Tony De <tonydema@gmail.com> wrote:

       ......................... it seems that all the page magic is coded into the
web page itself. I didn't see any info (perhaps that's not how JQuery
works) on accessing the DOM elements from a cgi or ruby code.

--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com

Hassan Schroeder wrote:

       ......................... it seems that all the page magic is coded into the
web page itself. I didn't see any info (perhaps that's not how JQuery
works) on accessing the DOM elements from a cgi or ruby code.

That's not how it works, period. JavaScript running in the browser
sends a request to a server process, which returns a response; it's
up to the in-page JS to handle the response appropriately.

But there's no way for the server to directly manipulate anything on
the client.

@ Hassan,

Ok, I get that part.
So, for example, looking at the examples on the JQuery website. When I
look at the source or any of the example page demos, which as I
understand it is using AJAX, I don't see any call to an external script.
It appears it is handling things locally, perhaps sending the request to
the "local" AJAX server, but not a a server script (say cgi) and the
script sending a request back to the AJAX server on the client.

tonyd

···

On Tue, Mar 18, 2008 at 1:51 PM, Tony De <tonydema@gmail.com> wrote:

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