views:

640

answers:

4

I currently have a link which when clicked calls a JavaScript function, which gathers some data and then uses ProtoType to submit a form...

function myFunction(){
  data = someobject.getData();
  $('myform').request({
    parameters: {data:data, id:id,},
    onSuccess: function(transport) { 
      document.location.reload();        
    }
  });  
}
...
<%= form_tag({:controller => "data", :action => "process"}, :id => "myform") %></form>

Notice my very unAJAXy document.location.reload(); in the onSuccess callback.

I want DataController#process to do this...

def process
  ...
  render :update do |page|
    page.replace 'my_div', :partial => 'test'
  end
end

Can anyone guide me as to what I need to change in the calling JavaScript for page.replace to work? Right now of course, it just gets ignored.

I've tried various things, and read around, but I'm not getting what I need to do in this situation.

Thanks

A: 

Hi Paul,

I'm not sure exactly how you want it to work, but there is a remote_form_for tag that will do the request for you. Also, you may wish to look at the link below for an example on using RJS templates:

http://www.developer.com/lang/article.php/3668331

Topher Fangio
+1  A: 

Take the rendering out of the controller and put this in views/datas/process.js.rjs

page.replace("my_div", :partial => "test")
chap
A: 

You could change your form_for to this:

<% form_remote_tag :url => {:controller => "data", :action => "process"},
                   :html => {:id => 'my_form'},
                   :update => 'my_form' do %>

And then render the partial in the controller and it will update 'my_form' with it:

def process
  ...
  render partial => 'test'
end
chap
A: 

Thanks for all suggestions - here's what I ended up doing:

function myFunction(){
  ...
  $('data').value = data;
  $('id').value = id;
  $('myform').request();  
}
...
<%= form_remote_tag 
  :url => {:controller => 'data', :action => 'process' }, 
  :html => {:id => 'myform'}%>
     <input type="hidden" id="data" name="data"/>
     <input type="hidden" id="id" name="id"/>
</form>

In DataController#process to do this...

def process
  ...
  render :update do |page|
    page.replace 'my_div', :partial => 'test'
  end
end

(chap - I'll put this in partial!)

Paul