Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1062991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:42:58+00:00 2026-05-16T18:42:58+00:00

I already have a form with normal HTTP post. Now, I want to rewrite

  • 0

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" }%>
  1. The request does not go in the jQuery.fn.submitWithAjax = function() { } method.

  2. 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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T18:42:58+00:00Added an answer on May 16, 2026 at 6:42 pm

    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:

    <%= form_tag :action=> "register_user", :remote => true %>
    

    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:

    <%= form_tag :action=> "register_user", :remote => true, :html => { :id => 'myform' } %>
    

    Now hook the events (with jQuery) like this:

    jQuery(document).ready(function() {
        jQuery('#myform').bind('ajax:complete', '', function(request) {
            alert("I got: " + request.responseText);
        });
    });
    

    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:

    <h2 id="myoutput">
      <% if @final_value != nil %>
        <%= @final_value.to_s %>
      <% end %>
    </h2>
    
    
    <% form_remote_tag :url => { :action=> "register_user" } do %>
    
      <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" %>
    
    <% end %>
    

    In your controller:

    def register_user
    
      form_data = params['user']
    
      if form_data.is_a?(Hash)
        @final_value = form_data['user_id_1']
      end
    
      respond_to do |format|
        format.html
        format.js do
    
          render :update do |page|
            page.replace_html 'myoutput', @final_value
            page.visual_effect :highlight, 'myoutput'
          end
    
        end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to assign a resource I already have a second name, similar to
For statistical reasons, I want an extensive analysis from a dataset. I already have
I have already posted something similar here but I would like to ask the
I have already googled for this I have a Table with following structure in
The basics have already been answered here . But is there a pre-built PHP
This may have been already asked but I can't seem to find this specific
I have this site I want to get information from. To get the file
I already have a deploy.rb that can deploy my app on my production server.
We already have things like static analysis that tells us what's wrong with our
What alternatives are there to GAE, given that I already have a good bit

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.