MotoHavoc
MotoHavoc

Reputation: 21

Trying to Perform Live AJAX Search in sidebar

I am new to AJAX searches and fairly new to Rails as well. I am trying to create a search form in the sidebar of my app so that it is included on every page. I need the search results to appear on the current page only when a search is entered.

I have a 'movies' table and am just starting off my searching for the 'title' column. Here is what I have so far. Right now the search isn't returning just the search results in the div, nor is it returning any results on the current page. Using Rails 4.0.0.

_sidebar.html.erb

    <aside id="sidebar">
<nav>
 <%= form_tag movies_path, :method => 'get', :id => "movies_search" do %>

<center>
<p>
 <%= text_field_tag :search, params[:search] %>
 <%= submit_tag "Search", :name => nil %>
</p>
</center>

 <div id="cresults_div" style="float:right; width:300px;"><%= render 'cresults' %></div>
<% end %>
 <ul style="margin-top: 300px;">
  <li class="all-movies">
    <%= link_to 'All Movies', movies_path, class: 'button' %>
  </li>
  <li class="create-movie">
    <%= link_to 'Add New Movie', new_movie_path, class: 'button' %>
  </li>
 </ul>

</nav>
</aside>

movies_controller.rb

  def index
 @movies = Movie.released
 @movies = Movie.search(params[:search])
 end
end

movie.rb

def self.search(search)
  if search
    where('title LIKE ?', "%#{search}%")
  else
    scoped
  end
end

_cresults.html.erb

<h2>Search Results: </h2>

<table>
 <tr>
  <th>Title:</th>
  <th style="width:85px;">Released: </th>
  <th>Rating: </th>
</tr>

  <% @movies.each do |movie| %>
<tr>
 <td><%= link_to movie.title, movie %></td>
 <td><center><%= movie.released_on.year %></center></td>
 <td><center><%= movie.rating %></center></td>
</tr>

 <% end %>
</table>

movies.js

$(function() {
 $('#movies_search').submit( ->
 $.get(this.action, $(this).serialize(), null, 'script')
  false
 });

$(function() {
 $('#movies_search input').keyup( ->
 $.get($("#movies_search").attr("action"), $("#movies_search").serialize(), null,   'script')
 false
  });
});

movies.js.erb

$("#cresults_div").html("<%= escape_javascript(render("cresults")) %>");

I also tried putting the same code from movies.js into application.js but had no change in results.

Upvotes: 1

Views: 138

Answers (1)

MotoHavoc
MotoHavoc

Reputation: 21

So I was able to figure it out. Hopefully this can help someone else out but here is what I changed:

Movies.js

 $(function () {  
  $('#movies_search input').keyup(function () {  
    if ($(this).val().length < 1 && e.keyCode != 13) return;
    $.get($('#movies_search').attr('action'), $('#movies_search').serialize(),    null, 'script');  
    return false;  
  });  
}) 

 $(function () {  
  $('button').click(function () {  
    $.get($('#movies_search').attr('action'), $('#movies_search').serialize(), null, 'script');  
    return false;  
  });  
})  

Application.js - Removed this require which was causing the AJAX search to only work on the index page.

//= require turbolinks 

Addition of index.js.erb

$("#cresults_div").html("<%= escape_javascript(render('cresults')) %>");

Changed up the _sidebar.html.erb file a little:

<aside id="sidebar">
 <nav>

<%= form_tag movies_path, :method => 'get', :id => "movies_search" do %>
 <center>
<p>
 <%= text_field_tag :search, params[:search] %>
 <%= submit_tag "Search", :name => nil %>
</p>
 </center>
<% end %>

<div id="cresults_div" style="margin-left: 13px; width:300px;"></div>

Upvotes: 1

Related Questions