Connecting Tech Pros Worldwide Forums | Help | Site Map

Alphabetic letter based search

Newbie
 
Join Date: Dec 2007
Posts: 10
#1: Mar 25 '09
Hi,

i have list of alphabets from A to Z .if i click on any one letter that should give me all the name starting with that corresponding letter from the database and should be displayed in the same page.

if i use each letter as a link(link_to) i cant display them in the same page.

if i use link_to_remote i am not able to get the params value of the letter i.e...Which letter i clicked.. if anyone know about this plz give me a solution.

Expert
 
Join Date: May 2007
Posts: 213
#2: Mar 25 '09

re: Alphabetic letter based search


You could try Rails AJAX support to send the database results to a div within the same page. Or you could try using a partial.
Newbie
 
Join Date: Dec 2007
Posts: 10
#3: Apr 3 '09

re: Alphabetic letter based search


Expand|Select|Wrap|Line Numbers
  1. <%= javascript_include_tag "prototype" %>
  2. <div class="element_links">
  3. <% for char in 'A'..'Z' %>
  4. <%= link_to_remote( "#{char}",
  5.                          :update => "content",
  6.                          :char => char,
  7.                          :url =>{:action => :alp_search }) %> </li>
  8.                          <% end %> 
  9.                          <div id="content"></div>
  10.                     <p>
  11. <ul>
This is my rhtml

and my controller is

Expand|Select|Wrap|Line Numbers
  1. def live_search
  2.      puts 'live_search'
  3.            @classifieds = Course.find(:all,:conditions => ["lower(course_name)like ?","%" + params[:search].downcase+ "%"])
  4.            puts @classifieds
  5.            if params['search'].to_s.size < 1
  6.            render :nothing => true
  7.            else
  8.                 if @classifieds.size > 0
  9.                    render :partial => 'display', :collection => @classifieds
  10.                    else
  11.                    render :text => "<li>No results found</li>", :layout => false
  12.                    end
  13.                 end
  14.               end

So as per the code if i click 'A' i should get all the courses which starts from 'A' from the database but am not able to get the params value of search.

In the place of link_to_remote if i use link i can get the correct result in the other page..but i want to display result in the same page..
is there is any way to do this...
Expert
 
Join Date: May 2007
Posts: 213
#4: Apr 3 '09

re: Alphabetic letter based search


Try adding content to your content div, such as
Expand|Select|Wrap|Line Numbers
  1. <div id="content">
  2.   <%= @content %>
  3. </div>
  4.  
Then, in your controller, set the content variable with the partial, like
Expand|Select|Wrap|Line Numbers
  1. def live_search
  2.   #...code...
  3.   @content = render_to_string(:partial => 'display', :collection => @classifieds)
  4.  
  5.   render :update do |page|
  6.     page.replace_html 'content', @content
  7.   end
  8. end
This should then update the div with your partial using your data collection.
Reply


Similar Ruby / Ruby on Rails bytes