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_value
data 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!