Hello to all of you,

I am using Jquery Auto-Complete Plugin with Rails.

My Task is: A text field where user will type(say "cri"), the auto
complete list will show the possible options (like "cricket"), once
cricket is selected the auto complete list will show another possible
options that comes under cricket (like "Matches", "Teams", "Scores"
etc.). How would I do that using jquery?

Right now I am using this code and able to get 1 level of records
successfully:

posts_controller.rb

class PostsController < ApplicationController
  def index
    @posts = []
    @post = Post.find(:all)
    @post.each do |post|
      @posts << post.title
    end
    @posts.inspect
  end
end

index.html.erb
<HTML>
 <HEAD>
  <TITLE> Auto complete with Jquery </TITLE>
  <script src="http://code.jquery.com/jquery-latest.js";></script>
  <link rel="stylesheet" href="http://dev.jquery.com/view/trunk/
plugins/autocomplete/demo/main.css" type="text/css" />
  <link rel="stylesheet" href="http://dev.jquery.com/view/trunk/
plugins/autocomplete/jquery.autocomplete.css" type="text/css" />
  <script type="text/javascript" src="http://dev.jquery.com/view/trunk/
plugins/autocomplete/lib/jquery.bgiframe.min.js"></script>
  <script type="text/javascript" src="http://dev.jquery.com/view/trunk/
plugins/autocomplete/lib/jquery.dimensions.js"></script>
  <script type="text/javascript" src="http://dev.jquery.com/view/trunk/
plugins/autocomplete/jquery.autocomplete.js"></script>
  <script>
  $(document).ready(function(){
  <% @posts.each do |posts| %>
    var data = "<%= posts %>".split(" ");
$("#example").autocomplete(data);
  <% end %>
  });
  </script>
 </HEAD>

 <BODY>
        <input id="example">
 </BODY>
</HTML>

Now there is another table called "categories" in my db and posts
has_many categories is the relationship between them.

Please help me how to get second level of records which will based on
1st level.

Thanks in Advance
Puneet Pandey

Reply via email to