DNorthrup DNorthrup - 3 months ago 17
Ruby Question

API Search on Rails Search Input

Probably poorly worded subject, I'm recently picking up Rails and trying to make some improvements on an old project.

I'm using a gem that handles the Steampowered API Calls - https://github.com/Olgagr/steam-web-api

And I'm rendering it with

<% if current_user %>
<% player = SteamWebApi::Player.new(current_user.player_steam_id) %>
<% data = player.owned_games(include_appinfo: true) %>
<% game = data.games %>
<% game.each do |f| %>
<%= f['playtime_2weeks'] %>
<% if f['img_icon_url'].empty? %>
<img src='http://www.readyicons.com/IconSets/Sky_Light_(Basic)/32x32-no.png'>
<% else %>
<img src='http://media.steampowered.com/steamcommunity/public/images/apps/<%= f['appid'] %>/<%= f['img_icon_url'] %>.jpg'>
<% end %>
<%= f['name'] %><br>
<% end %>
<% end %>


I added in Active Directory record and modified it, so that it auto populates the field in the users category with the data.

Essentially I'm trying to do the same thing next to it, but with a search. "current_user.player_steam_id" would be replaced with a specific query return.

Right off the bat, even if I could get it to work (I was resorting to using JS Swaps, which worked, but wasn't the way I feel like it should work) I would literally have two of the same code blocks next to each other.

I believe at this point is when I would use a form_helper and place this in a form, and render it... but how do I render it dynamically? I've been reading a lot of the Active:Directory:RoR Tutorials, and I used it to make my create fields, but I'm unsure how to make it for a dynamic section.

For small example.

Before:

| Section 1 | Section 2 | Section 3|
|CurrentUsersGames|Steam# To Search? | TBD |
|CurrentUsersGames| [Input Field] | TBD |
|CurrentUsersGames| | TBD |
|CurrentUsersGames| | TBD |
|CurrentUsersGames| | TBD |
|CurrentUsersGames| | TBD |
|CurrentUsersGames| | TBD |


After Search:

| Section 1 | Section 2 | Section 3|
|CurrentUsersGames|SearchedUsersGames| TBD |
|CurrentUsersGames|SearchedUsersGames| TBD |
|CurrentUsersGames|SearchedUsersGames| TBD |
|CurrentUsersGames|SearchedUsersGames| TBD |
|CurrentUsersGames|SearchedUsersGames| TBD |
|CurrentUsersGames|SearchedUsersGames| TBD |
|CurrentUsersGames|SearchedUsersGames| TBD |


Would appreciate any suggestions. Thanks.
(The <% if current_user %> stops it from breaking if they're not logged in)

Edit: Added 'before' and 'after' clarification. Essentially I'm trying to get the middle div to go from a search field to the results of the API response.

Answer

If you're mainly after de-duping your code in your view, then probably what you want is a partial template.

here's an example of what you'd have in the original template:

<% if user_logged_in? %>
  <% player = SteamWebApi::Player.new(current_user.player_steam_id)
     owned_games = player.owned_games(include_appinfo: true).game
     search_games = player.your_game_search_here %>

 <%= render :partial => 'game_list', :locals => {:games => owned_games, :header => "Games owned by #{player.name}"} %>
 <%= render :partial => 'game_list', :locals => {:games => search_games, :header => "Filtered Game search list title here"} %>

<% end %>

and here's what would go in your partial (which you'd name _game_list.html.erb in this example)

<h1><%= header %></h1>
<% games.each do |game| %>
  <%= game['playtime_2weeks'] %>
  <% if game['img_icon_url'].blank? %>
    <%= image_tag 'http://www.readyicons.com/IconSets/Sky_Light_(Basic)/32x32-no.png' %>
  <% else %>
    <%= image_tag "http://media.steampowered.com/steamcommunity/public/images/apps/#{game['appid']}/#{game['img_icon_url']}.jpg" %>
  <% end %>
  <%= game['name'] %><br>
<% end %>