views:

30

answers:

1

Hi all,

Sorry if it looks stupid question but is it possible to return the response from a JavaScript confirmbox or messagebox to a ruby program as a value?

I click on confirmbox's "yes" button, this response is stored in a variable, and then this variable is returned to ruby?

A: 

Sure, it´s easy. Just send an XHR back to the server. If your are using jQuery you could do something like this:

$.getJSON('/path/to/check', {client_data: stuff}, function(response) {
     //e.g. server returns
     if (response.ask && window.confirm("Sure?") || !response.ask) { 
         sendMyStuffToServer();
     }
})

An XHR request (aka AJAX) is basically the same as a normal one except that it's performed in background (which looks like what you are looking for).

glebm
Thanks glebm but could you please give me an exapmle hoe i call javascript from ruby and return confirmbox response back to ruby or you just give me some document to read how do call js form ruby and all these this because i don't jquery and all those thins
AMIT
It is very difficult to call JS form from Ruby (not sure what you mean here). For jQuery doc look here: http://api.jquery.com/jQuery.get/
glebm
but is it possible to call JS from ruby or not??
AMIT
What is exactly the problem you are trying to solve?
glebm
Actully glebm in my rails appilation am looking tp popup a msgbox for that i used win32api messagebox but its poping up ony on server side not on client side so now would like to know how call JS from ruby and popup the msgbox on client side here is my code
AMIT
http://www.ruby-forum.com/topic/275384#new
AMIT
In my given code i would like to call javascript instead of messagebox
AMIT
But surely that message box pops up as a result of some action from the user? Or you want to show it right after the page loads?
glebm
OK, I looked at your code (for a bit, cause it's scary). You can ask the server with AJAX, *get the response*, *and then display the box if necessary*
glebm
as soon as this condition is true i would like to call js is it possible if $L=(name.eql?(k[i]) == true)after that condition i would like to see that confirm box and thats box respone should be get in my ruby code is it possible glebm??
AMIT
The idea is to make two separate actions. One would check your condition and return the result to the client (this is the action you'd call asynchronously). And then you can have the second action which javascript would call if the user clicked OK in the confirm box.
glebm
any way Thanks glebm for your kind help i will try what you said and let you know its is working or not??
AMIT
then where i call my
AMIT
post = DataFile.save(params[:upload])
AMIT
For the first action you can subscribe to `change` event on the file upload input. For the second action is the actual upload in your case.
glebm
could you please explain how i did n't get you or by some example
AMIT