API搜索Rails搜索输入 [英] API Search on Rails Search Input
问题描述
可能是措辞不佳的主题,最近我正在学习Rails,并尝试对旧项目进行一些改进.
Probably poorly worded subject, I'm recently picking up Rails and trying to make some improvements on an old project.
我正在使用处理Steampowered API调用的宝石- https://github.com/Olgagr/steam-web-api
I'm using a gem that handles the Steampowered API Calls - https://github.com/Olgagr/steam-web-api
我正在用
<% 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 %>
我在Active Directory记录中添加并修改了它,以便它自动用数据填充用户类别中的字段.
I added in Active Directory record and modified it, so that it auto populates the field in the users category with the data.
基本上,我正尝试在它旁边执行相同的操作,但是要进行搜索. "current_user.player_steam_id"将替换为特定的查询返回.
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.
即使我可以使用它,也可以立即使用(我一直在使用JS Swaps,该方法很有效,但是我觉得它不应该工作),我确实会有两个相同的代码彼此相邻.
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.
我相信在这一点上,我将使用form_helper并将其放置在表单中并进行渲染...但是如何动态地渲染它?我已经阅读了很多Active:Directory:RoR教程,并且使用它来创建自己的创建字段,但是不确定如何在动态部分中使用它.
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.
举个小例子.
之前:
| 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 |
搜索后:
| 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 |
将感谢您的任何建议.谢谢. (如果当前用户未登录,则<%如果current_user%>阻止其中断)
Would appreciate any suggestions. Thanks. (The <% if current_user %> stops it from breaking if they're not logged in)
添加了之前"和之后"的说明.本质上,我试图使中间div从搜索字段转到API响应的结果.
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.
推荐答案
如果您主要是在将视图中的代码重复数据删除之后,则可能需要的是部分模板.
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 %>
,这就是您的部分内容(在此示例中,您将其命名为_game_list.html.erb
)
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 %>
这篇关于API搜索Rails搜索输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!