I have installed ransack and it seems easy to use. However when I click search it just refreshes the page, the proper data is not shown. On the page I have the search box with listing all registered usernames (admin and user). For the search box I entered 'admin' and when I click search the page reloads and I continue to see all registered users (admin and user). It should be displaying 'admin' only after the search. Any idea as to why this is happening?
users_controller:
def index
@search = User.search(params[:q])
@users = @search.result
@users = User.all
end
index.html:
<%= search_form_for @search do |f| %>
<div class="field">
<%= f.label :username_cont, "Name contains" %>
<%= f.text_field :username_cont %>
</div>
<div class="actions"><%= f.submit "Search" %></div>
<% end %>
<% @users.each do |user| %>
<li>
<%= link_to user.username, user %>
<% if current_user.admin? || current_user == @user %>
<%= link_to "Edit #{user} profile", user %>
| <%= link_to "delete", user, method: :delete,
data: { confirm: "You sure?"} %>
<% end %>
</li>
<% end %>
You should be getting all the results not search results... You are over-riding the instance variable
this:
@users = @search.result
@users = User.all
should just be:
@users = @search.result
or better yet:
@search = User.search(params[:q])
if params[:q].present?
@users = @search.result
else
@users = User.all
end
All being said... Is your form submitting a param for "q"? I don't know ransack but I think your form also needed changing.