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

  • SEARCH
  • Home
  • 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 7054337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:36:51+00:00 2026-05-28T03:36:51+00:00

Im using rails 3.1, and I am creating a search form with ajax, I

  • 0

Im using rails 3.1, and I am creating a search form with ajax, I have already the form working with a post method, I am missing something here…
So in my view I have

<%= form_tag(root_path, :method => "post",:id =>"formid") do %>
  <%= label_tag(:number1, "El que quiero") %>
  <%= text_field_tag :quiero, nil, :class => 'drug_autocomplete' %>
  <%= hidden_field_tag :number1 %>

  <%= label_tag(:number1, "El modelo que tengo") %>
  <%= text_field_tag :tengo, nil, :class => 'drug_autocomplete' %>
  <%= hidden_field_tag :number2 %>

  <%= label_tag(:size1, "Talla de el que tengo") %>
  <%= text_field_tag :size1%>

  <%= submit_tag("Do it!") %>

<% end %>

Ok and I wrote in the application.js something to handle the submit event.

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() {
  $("#form_id").submitWithAjax();
})

The controller recibes the parameters in the form, and create a query to the database like this.

@itemsok = Contribution.where("first_item_id = ?",item1.id).where("second_item_id = ?",item2.id).where("second_item_grade = ?",size1

And then returns the parameters found in the db in the same view

<%if not @itemsok.nil? 
  @itemsok.each do |searches|
%>
<table>
<tr>
  <td style="width:100px;"><%= searches.first_item.description %>  </td>
  <td style="width:150px; font-size:30px;"><%= searches.first_item_grade %>  </td>


  <td style="width:150px;"><%= searches.second_item.description %> </td>
  <td style="width:150px; font-size:30px;"><%= searches.second_item_grade %>  </td>
  <td style="width:150px; font-size:18px;"><a href="<%= contribution_path(searches.id) %>">Show</a>  </td>


</tr>
</table>

What I want to achieve is the same creating a query to the database but with ajax so no reload in the page is needed.
First question, I have to handle the data from the form somewhere in the javascript, where?
Second question, how do I handle the data when is back

I am totally lost in Ajax with Rails, so any help/hint would be very appreciated.
Thanks in advance.
Sorry for my terrible english.

  • 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-28T03:36:52+00:00Added an answer on May 28, 2026 at 3:36 am

    Your function to submitWithAjax is calling serialize() which prepares the form data for you.

    Your controller for the form action will need to respond to format.js ABOVE format.html and you will need a file matching the action name in the appropriate view folder with the “.js.erb” extension.

    Within that “.js.erb” file you can call arbitrary jQuery to replace content, change elements of the page, or trigger actions.

    $('#lightBoxClose').trigger('click');
    $('#flashContentContainer').html('<%= escape_javascript(render :partial => "shared/flash_bar") %></div>');
    

    One other thing is to make sure your application responds with the format.js action you can change your submitWithAjax function like so:

    jQuery.fn.submitWithAjax = function() {
      this.submit(function() {
        $.post(this.action + '.js', $(this).serialize(), null, "script");
        return false;
      })
      return this;
    };
    

    Notice the ‘.js’.

    If you haven’t seen it check out this railscast

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Rails 3.2. I have an individual Shop post, and each shop has reviews.
I am creating a new rails app as a learning project. I have already
Using Rails 3.0.9, I'm trying to redirect after a post to the create method
I'm using Rails and I have a User Controller for creating new users. To
Novice here! I'm currently creating an application using Ruby on Rails. This particular application
I am working on creating a adserver using rails 3.1 and ruby 1.9.2. In
I am creating a Rails application with TDD using rspec. I am getting an
I am using ruby on rails 3.2.3 and capybara to help creating request spec.
I'm creating a website in Ruby on Rails, where users can login using RESTful
I creating a site in JRuby on Rails (JRuby 1.3.0 ). I am using

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.