cldwalker
2006-Nov-28  19:10 UTC
using Javascript''s prompt() and render_component to make one Rails action interactive
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 %>
     <script type="text/javascript">
       var answer = prompt(''<%= @params[:prompt]
%>'','''');
       /* new Ajax.Request(''/files/js_save?answer='' + answer,
       {asynchronous:true, evalScripts:true}); */
     </script>
    ]
  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
--~--~---------~--~----~------------~-------~--~----~
 You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---