Is there a way to submit ajax/json requests with simple_form for Rails

后端 未结 5 1958
感情败类
感情败类 2021-01-05 09:43

With the standard Rails form_for, I was able to pass ajax requests though select and collection_select helpers as such:

<%= address.collection_select :cou         


        
5条回答
  •  Happy的楠姐
    2021-01-05 10:42

    I wanted to post a related follow up, because I had some difficulty figuring out how to implement the callback for this. I found this article to be very helpful: http://www.simonecarletti.com/blog/2010/06/unobtrusive-javascript-in-rails-3/

    The secret is either to add an HTML5 data attribute, e.g. data-complete or (better) bind the ajax:complete or ajax:success callbacks provided by Rails to your form (see 4. Remote JavaScript Callbacks in article linked above):

    From the article:

    jQuery(function($) {
      // create a convenient toggleLoading function
      var toggleLoading = function() { $("#loading").toggle() };
    
      $("#tool-form")
        .bind("ajax:loading",  toggleLoading)
        .bind("ajax:complete", toggleLoading)
        .bind("ajax:success", function(event, data, status, xhr) {
          $("#response").html(data);
        });
    });
    

    CoffeeScript example:

    $("#new_post").on("ajax:success", (e, data, status, xhr)->
      console.log data
    )
    

提交回复
热议问题