问题描述
可能是措辞不佳的主题,我最近正在学习 Rails 并试图对一个旧项目进行一些改进.
我正在使用处理 Steam 驱动的 API 调用的 gem - 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 记录中添加并修改了它,以便它使用数据自动填充用户类别中的字段.
基本上我正在尝试在它旁边做同样的事情,但是通过搜索."current_user.player_steam_id"将替换为特定的查询返回.
马上开始,即使我可以让它工作(我正在使用 JS Swaps,它有效,但不是我认为它应该工作的方式)我实际上会有两个相同的代码块彼此相邻.
我相信此时我会使用 form_helper 并将其放置在表单中并渲染它...但是如何动态渲染它?我已经阅读了很多 Active:Directory:RoR 教程,并使用它来创建我的字段,但我不确定如何将它用于动态部分.
举个小例子.
之前:
| 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 |
如果有任何建议,我们将不胜感激.谢谢.(<% if current_user %> 如果他们没有登录则阻止它中断)
编辑:添加了"之前"和"之后"的说明.本质上,我试图让中间 div 从搜索字段转到 API 响应的结果.
推荐答案
如果您主要是在视图中对代码进行重复数据删除之后,那么您可能想要的是部分模板.
以下是原始模板中的示例:
<% 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)
<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 %>
问题描述
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.
推荐答案
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 %>