All threads / Displaying search results from an API
Ask A Question

Notifications

You’re not receiving notifications from this thread.

Displaying search results from an API

OnRice asked in Rails

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?

    Join the discussion

    Want to stay up-to-date with Ruby on Rails?

    Join 56,919+ developers who get early access to new tutorials, screencasts, articles, and more.

      We care about the protection of your data. Read our Privacy Policy.

      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.