I am trying to put a search facility on a Users Index page in a Rails application (similar to that in Michael Hartl's Rails Tutorial) and am using the Ransack gem to do the search. It's just a simple search on the user's name that I've implemented as follows:
Users Controller:
def index
@title = "All Users"
@search = User.search(params[:q])
@users = @search.result.paginate(page: params[:page])
end
Index View:
<%= search_form_for @search do |f| %>
<%= f.text_field :name_cont, id: :search_box %>
<%= f.submit "Search" %>
<% end %>
The problem is this: when I go into the Index page and search for a user that actually appears there, they might not come up. For instance, Mrs. Linnie Bahringer appears in the original list, but not in the search results when I search for 'lin'.
[I wanted to show pictures here, but can't as I need five more reputation points.]
Naturally, I thought that Ransack was somehow botching the query. However, when I output the @users variable to the page, by putting
<%= @users.each do %>
<% end %>
into the view, the 'missing' user is in the list of names and, lo and behold, appears in the list of results!
Does anyone know why this might be happening and how I can end up with the 'right' set of results without printing out the contents of the @users variable to the screen? I have tried taking the '=' sign out of the @users.each do
line as a hack, but the behaviour returns to that mentioned in the problem above.
Thanks very much in advance for any help you can give!
PS I should probably show my entire view file just in case it might be of use:
<% provide :title, 'All Users' %>
<h1>All Users</h1>
<div class="span3 offset9 btn-group">
<%= search_form_for @search do |f| %>
<%= f.text_field :name_cont, id: :search_box %>
<%= f.submit "Search" %>
<% end %>
</div>
<%= will_paginate %>
<!-- This bit is only in to show the value of @users -->
<%= @users.each do %>
<% end %>
<!-- Extra bit ends. -->
<ul class="users">
<%= render @users %>
</ul>
<%= will_paginate %>