0

I am running a web page from a server using webrick. How can I capture an event from the web browser? I do not want to do it through JavaScript, but I want to get a response through webrick, and process it in ruby, perhaps using one of the do_... methods. If JavaScript is minimally needed, that is okay. I do not want to redirect to a different page. I do not know what to put in the value for an event (like onclick).

<input type=button onclick="..." value="Press Me">
sawa
  • 165,429
  • 45
  • 277
  • 381

3 Answers3

0

AFAIK, Webrick is HTTP server in Ruby. In order to communicate with HTTP server from event handler, you'll need to write some JavaScript code. For example:

<input type=button onclick="javascript:send(this);" value="Press Me">

And then define send() function:

function send(event) {
   // Create Ajax request to the server
}
Michael Spector
  • 36,723
  • 6
  • 60
  • 88
0

Sounds like you are describing ajax. Try:

link_to "target", {:controller => controller, :action => method, }, :remote => true

from this SO answer:

Where did link_to_function disappear to in Rails 3?

Edit: Sorry, I assumed Rails when you were asking for Ruby.

Community
  • 1
  • 1
seph
  • 6,066
  • 3
  • 21
  • 19
  • I am not using Rails. I am just trying to do it by myself. But if I can know how it is implemented in Rails, that can help. – sawa Jun 13 '11 at 04:38
0

After struggling, with the help of this page, I seem to have gotten close to what I wanted:

xxx.html

<input type="button" value="Click me" onclick="onclick('value to pass');" />

xxx.js

function onclick(v){
  e=new XMLHttpRequest();
  e.open("GET", "http://localhost:3000?t=0&"+v, true);
  e.onreadystatechange=function(){if (e.readyState==4 && e.status==200){
    // some code to rewrite if necessary
  };};
  e.send();
}

xxx.rb

class WEBrick::HTTPServlet::AbstractServlet
  def do_GET request, response
    # some code to process the request
    response.body = a_return_string_for_rewriting
    response.status = 200
  end
end
sawa
  • 165,429
  • 45
  • 277
  • 381