简体   繁体   中英

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?

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'.

I might guess this post can help you. Reload Partial (Rails Ajax)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM