Ruby on rails 在Rails中显示来自API的搜索栏查询结果

Ruby on rails 在Rails中显示来自API的搜索栏查询结果,ruby-on-rails,api,search,Ruby On Rails,Api,Search,我试图创建一个简单的应用程序,你可以通过搜索栏从API中找到游戏。我正在使用巨型炸弹API gem()。gem似乎在工作,因为我可以通过控制台调用东西。但是,我不太确定如何显示结果,或者我的搜索方法是否有问题(可能) 这是我的游戏控制器: class GamesController < ApplicationController //Display search results def index @games = Game.all.order('created_at DESC'

我试图创建一个简单的应用程序,你可以通过搜索栏从API中找到游戏。我正在使用巨型炸弹API gem()。gem似乎在工作,因为我可以通过控制台调用东西。但是,我不太确定如何显示结果,或者我的搜索方法是否有问题(可能)

这是我的游戏控制器:

class GamesController < ApplicationController
//Display search results

  def index
  @games = Game.all.order('created_at DESC')
  @games = @games.search(params[:query]) if params[:query].present?
  end

//Searches through API and redirects to results on index.
  def search
    @games = GiantBomb::Search.new().query(params[:query]).resources('game').limit(5).fetch
    redirect_to index_path
  end


private

  def game_params
    params.require(:game).permit(:name)
  end
end


redirect\u to
创建一个新请求,您需要使用
render
如中所示

render 'index'
这样,您就可以在视图中使用
@games
变量

<ul>
  <% @games.each do |game| %>
    <li><%= game.name %></li>
  <% end %>
</ul>
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
render 'index'