Search code examples
jqueryruby-on-railspartial

Using ajax on a rendered partial in Rails 3


I am using Rails 3 to load a partial (form) into another page with the code below. The problem is that I want to use :remote => true on the form and use ajax upon it (validation) etc.

$("#form_open").bind("ajax:complete", function(et, e){

    $("#form_area").html(e.responseText);
    $("#form_open").html("Add contact information");

});

This does not seem to be possible. Why can't I use ajax in this situation?


Solution

  • If I understood the question, I think what you want to do is to have the ajax call to render a partial into some element in the current page right?

    In the current view:

    <%= link_to 'Add Contact Info', add_contact_info_path, :remote => true %>
    

    Obviously you have to get add_contact_info_path to route to an action, that action should render some js.erb. That js.erb should output some javascript that updates the #form_area and #form_open. So you could have an add_contact_info.js.erb (in jQuery):

    $('#form_area').html("<%= escape_javascript(render('somepartial')) %>");
    $('#form_open').html("Add contact information");
    

    That will render the "_somepartial.html.erb" partial into whatever element has the id = 'form_area'.