Hi,
I’m trying to use a combination of render_component and Javascript’s
prompt() to make one Rails action interactive. This means I could ask
for input via Javascript and then pass the Javascript answer to some
Ruby code. Is this possible?
For example:
======
class FilesController < ApplicationController
def js_save
$answer = params[:answer]
render :nothing=>true
end
def js_ask
render :inline=>%[
<%= javascript_include_tag :defaults %>
]
end
def some_action
a = “default value”
render :inline=>%[
<%= render_component :action=>‘js_ask’, :params=>{
:prompt=>‘1st value of a?’} %>
<%# set a to the new answer, $answer %>
<%# a = $answer %>
<%# render_component :action=>'js_ask', :params=>{
:prompt=>'2nd value of a?'} %>
], :layout=>true
end
end
======
Note: Try the above with /files/some_action
My main problem is that I can’t get back the JS variable answer while
I’m still in some_action(). Possible ways I thought of returning
answer:
- an Ajax request (commented out), passing answer as a parameter:
This doesn’t work since the request is executed
after some_action(). - a JS redirect (window.location.href = ‘’, passing answer as a
parameter: Same as above. - writing to a file/database or using a
system call and have Ruby read from wherever JS wrote: I’ve read
that JS file manipulation and system calls aren’t possible for
security reasons. Even if I could this, seems like a convoluted way
of passing a value.
Given my limited knowledge of JS, is there some obvious way I’m
missing to return a JS value to Ruby within a Rails action?
If you’re wondering why I have the one Rails action constraint, it’s
due to using callcc() (not shown above) which only works within one web
request/action.
Otherwise, I get threading errors.
Thanks,
Gabriel