OnRice

Joined

50 Experience
0 Lessons Completed
0 Questions Solved

Activity

Posted in Displaying search results from an API

I am working on a small project where users can search through an API (https://github.com/games-directory/api-giantbomb) via a search bar. However, nothing is displaying on my index page with the results.

Here is my controller:

class GamesController < ApplicationController

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

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 index renders after a search, but the results aren't there. Here is my index page:

<% @games.each do |game| %>

  • <%= game.name %>

  • <% end %>

    What needs to be changed with my index function to display the data, or is the issue somewhere else?

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2022 GoRails, LLC. All rights reserved.