I already have a form with normal HTTP post. Now, I want to rewrite this form usign jQuery Ajax request. Here is what I have:
register_user.html.erb
<%= form_tag :action=> "register_user" %>
<label for="user_id_1">user_id_1:</label><br/>
<%= text_field "user", "user_id_1", :size => 20 %>
<label for="user_id_2">user_id_2:</label><br/>
<%= text_field "user", "user_id_2", :size => 20 %>
<%= submit_tag "Submit" %>
users_controller.rb
def register_user
#do something
end
So, I was trying to follow this tutorial, but was not able to follow the same instructions in my app. So any suggestions? How can I make jQuery Ajax call using the above form?
AFAIK, the application.js (file included in register_user.html.rb’s head) file will be
jQuery.ajaxSetup({
'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")}
})
jQuery.fn.submitWithAjax = function() {
this.submit(function() {
$.post(this.action, $(this).serialize(), null, "script");
return false;
})
return this;
};
$(document).ready(function() {
$("#new_review").submitWithAjax(); #WHAT SHOULD REPLACE #new_review?
})
But, how can I associate the submit action to the form? I am new to JS, jQuery and Rails.
UPDATE 1
Rails 2.3.8
UPDATE 2
<%= form_tag :action=> "register_user" , :html => {:id => "register_user" }%>
-
The request does not go in the
jQuery.fn.submitWithAjax = function() { }method. -
How can I display the
@final_valuedata on the same page?
For now, this is my code to display the value (in register_user.html.erb):
<%=
if @final_value != nil
"<h2>The Output is " + @final_value.to_s</h2>" +
end
%>
Thanks for the awesome help guys!
Are you on rails 3? Rails3 supports unobtrusive javascript (UJS). Look at the file public/javascripts/rails.js
How does it work?
To create a remote form, do this:
this adds a data-remote attribute to your form.
Now the rails-built-in UJS hooks in and does the onsubmit thing for you. It fires events like:
ajax:before, ajax:complete, ajax:success, ajax:failure, ajax:after
Rails uses prototype and not jQuery by default, so you could (don’t have to) replace prototype by jQuery. Read http://www.railsinside.com/tips/451-howto-unobtrusive-javascript-with-rails-3.html
The important part now is, you can catch these events on your form! Give your form an id to identify it:
Now hook the events (with jQuery) like this:
you can also hook other events like ajax:failure etc.
Edit
For Rails 2.3.8 (make sure you’ve included the rails default js files)
Note if you want to use jquery only and drop the default included prototype library, you could use jRails (replaces prototype with jQuery): http://github.com/aaronchi/jrails
I don’t know what your @final_value is, so I did a generic example:
In your view:
In your controller: