[Edit] Changed some functions to make it more clear. I am trying to make a search function where the user can input a query, and an API(https://github.com/games-directory/api-giantbomb) returns matching items. However nothing is appearing on my Index despite there not being any errors popping up when I search.
I decided to use the Rails LIKE feature to return the query results in my index as seen below.
def index
if params[:query].present?
sql_query = "name LIKE :query"
@games = Game.where(sql_query, query: "%#{params[:query]}%")
else
flash[:notice] = "No Games Found"
end
end
It redirects to the Index, but shows no games nor the flash message. Here is the index: My index.html.erb that is supposed to return a list of games that match the query:
<h1>Test</h1>
<ul>
<% @games.each do |game| %>
<li><%= game.name %></li>
<% end %>
</ul>
I can see the "Test" text, but nothing else after I conduct a search.
For reference, here is the rest of my GamesController.
class GamesController < ApplicationController
//Displays search results
def index
if params[:query].present?
sql_query = "name LIKE :query"
@games = Game.where(sql_query, query: "%#{params[:query]}%")
else
flash[:notice] = "No Games Found"
end
end
//Searches through API data
def search
@games = GiantBomb::Search.new().query(params[:query]).resources('game').limit(5).fetch
render 'index'
end
private
def game_params
params.require(:game).permit(:name, :search, :query)
end
end
My routes just in case that is having any effect.:
Rails.application.routes.draw do
devise_for :users
resources :games
root to: 'pages#home'
get '/search', to: 'games#search', as: :search
get '/games', to: 'games#index', as: :index
end
My search bar on my home page:
<div class="search-font"><h1>Build your Collection</h1></div>
<div class="container">
<div class="row">
<div class="col-md-12 row">
<%= form_tag index_path, method: :get do %>
<div class="col-12 col-sm pr-sm-0">
<%= text_field_tag :query,
params[:query],
class: "form-control input-lg",
id: "typed-text" %>
</div>
<div class="input-group-btn ml-3">
<%= submit_tag "Search", class: "btn btn-primary" %>
</div>
<% end %>
</div>
</div>
</div>
What's the issue here? Do I need to revise my index method or is there another problem?