Question

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 %>
Was it helpful?

Solution

Well, I feel a bit of a chump. After working on another problem for a while, it occurred to me that it might just be the display of the information that was wrong. Sure enough, when I checked the HTML source (which I'm surprised I forgot to do before posting the question), the 'missing' user was in it.

I solved the matter by putting a non-breaking space in a div above the list (pretty much where the @users.each do bit is) then setting the CSS for that div to have a clear: both; in it.

Here's my updated view:

<% 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>

<div id="clear-space">
&nbsp;
</div>

<div id="all_users_list">
<%= will_paginate %>
<ul class="users">
    <%= render @users %>
</ul>
<%= will_paginate %>
</div>

As I say, it was a silly mistake to make, but we're all chumps at least once a day!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top