deruse
deruse

Reputation: 2881

rails 3 - pre-processing and adding inputs fields before ajax form is submitted

I have a AJAX form in Rails 3. Given the nature of my app, I need to call .submit() on the form programatically. But before the AJAX request is sent to the server, I want to perform some client side logic on the form data. And in this logic, I add hidden input fields inside the form being submitted.

So I have two questions:

  1. what is the recommended way to do this client side pre-processing upon the submission of an AJAX form which is Rails friendly?
  2. will the hidden input fields I add during this pre-processing be successfully sent in the POST request, as intended?

Thanks.

UPDATE -- I answered question 1 by using the .submit() event handler. But it looks like the hidden input field I add to the form in that logic is not being sent in the POST request. Any ideas on how I can overcome this?

Upvotes: 0

Views: 662

Answers (1)

Rishav Rastogi
Rishav Rastogi

Reputation: 15492

Its quite simple, you can use the jQuery .submit() event handler as well.


$('form_selector_here').submit(function()  {

   // pre process form here, add fields using $.append() etc
   $(this).ajaxSubmit();

   // or
   $.ajax({
    // settings here;   
   })

 })

For .ajaxSubmit() , you need to use the Jquery Form Plugin http://jquery.malsup.com/form/#getting-started

Upvotes: 1

Related Questions