new to rails here. I have my normal index page for a User controller below.
I just added search to my app with search kick and elastic search.
How would I merge the two separate blocks of code to work together in one view (index.html.erb)? I'm using a _user partial for the final render.
Currently I'm getting this error because they aren't integrated correctly.
NoMethodError in UsersController#index
undefined method `each' for nil:NilClass
<div class="container-fluid">
<% @users.each do |user| %>
<div class="col-md-4">
<%= render user %>
</div>
views/user/index.html.erb
Search Block
<div class="books-search">
<h1>Search Books</h1>
<%= form_tag users_path, method: :get do %>
<div class="form-group">
<%= text_field_tag :query, params[:query], class: 'form-control' %>
<%= submit_tag 'Search', class: 'btn btn-primary' %>
</div>
<% end %>
</div>
Normal Index Block
<div class="container-fluid">
<% @users.each do |user| %>
<div class="col-md-4">
<%= render user %>
</div>
<% end %>
</div>
<%= will_paginate %>
User Controller
def index
if params[:query].present?
@user = User.search(params[:query])
else
@users = User.paginate(page: params[:page])
end
end
When you send a search param your controller will do:
@user = User.search(params[:query])
while view expects to have @users
variable, not @user
. @users
is nil.