在 Rails 4.2 中将区域设置变量推送到部分 [英] Pushing a Locale Variable to Partial in Rails 4.2

查看:49
本文介绍了在 Rails 4.2 中将区域设置变量推送到部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个部分可以提取某人在其 Steam 库中的游戏列表

I have a partial that pulls the list of games someone has in their Steam Library

<h1><%= header %></h1>
<% player = SteamWebApi::Player.new(steamId)
 owned_games = player.owned_games(include_appinfo: true)%>
<% owned_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 %>

它的基本视图来自 current_user.player_steam_id - SQL 中的一个字段.本来这很好,只展示你现在的游戏,但我想用它做一些事情.

And it's base view feeds off of current_user.player_steam_id - A field in SQL. Originally this was nice to show only your present games, but I want to do a few things with it.

A) 当您查看某人的个人资料时,您会看到他们的列表.B) 您可以使用用户 ID 进行搜索,以拉取查询结果.

A) When you check someones profile, you see their list. B) You can search with a users ID, to pull the query results.

我最初的问题是使用(XXXXXX 实际上是一个数字,所以我不对其进行字符串化(是吗?)

My initial issue is using (XXXXXX is actually a number, so I don't stringify it(Do I?)

   <%= render :partial => 'game_list', :locals => {:header => "Welcome to His Page!", :steamId => XXXXXXXXXX } %>

还有欢迎来到他的主页!"正在通过,但我在渲染 :steamId 时遇到了很多问题.

And the "Welcome to His Page!" is getting passed, but I'm having a lot of issues getting :steamId to render.

在局部视图中,我尝试使用 <%= steamId %> <% steamId %>:steamId

In the partial view I've tried rendering it with <%= steamId %> <% steamId %> and :steamId

current 和 :steamId 是仅有的两个不会破坏页面,但它不会呈现任何内容.我对此很陌生,所以想弄清楚,但我发现很多帖子都说我做得对.

The current and :steamId are the only two that don't break the page, but it doesn't render anything. I'm fairly new to this so trying to figure it out, but I've found numerous posts that say I'm doing it right.

欢迎提出任何意见.

另外 - 由于我计划广泛使用它,我是否应该考虑将其设为控制器?或者部分就足够了?(PS 我通常是一个 Node 人,所以我尝试了 debug+console.log(steamId) 很多来看看它是如何渲染的,但我没有在 console.log 中得到结果,并且 debug 没有做任何事情.如何你通常会在 RoR 中调试类似的东西吗?

Also - Since I plan to use this widely, should I look into making it a Controller? Or would the partial suffice? (P.S. I'm typically a Node guy, so I tried debug+console.log(steamId) a lot to see how it's rendering, but I didn't get results in console.log, and debug didn't do anything. How do you normally debug something like this in RoR?

感谢您的任何意见!

附加信息:

这个问题是部分原因.

<h1><%= header %></h1>
<% player = SteamWebApi::Player.new(steamId)
owned_games = player.owned_games(include_appinfo: true)%>
<% owned_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 %>

通过修改为owned_games定义,并且玩家定义在不同的行,解决成功.就我而言,这只是一个没有审查我复制/粘贴的内容的失败.

By modifying it to owned_games definition, and player definition are on separate lines, it resolved successfully. This was just a not-reviewing-what-I-copied/pasted fail on my part.

解析代码:

<h1><%= header %></h1>
<% puts steamId %>
<% player = SteamWebApi::Player.new(steamId) %>
     <% owned_games = player.owned_games(include_appinfo: true)%>
     This is my steam ID <%= steamId %>
<% owned_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 %>

但是,如果它从数据库中提取 player_steam_id,我只能让它呈现.

However, I am only able to have it render if it pulls the player_steam_id from the DB.

例如,如果我有<代码>:steamId =>76561198017108025

这行不通,但如果我有

:steamId =>User.player_steam_id 确实有效.

(同样,'User' 在 SQL 中有一个列存储他们的 player_steam_id)

(Again, 'User' has a column in SQL that stores their player_steam_id)

我希望能够在搜索按钮的提交"上调用它,这就是为什么我试图了解如何让渲染与特定数字一起工作.

I want to be able to call this on the 'Submit' of a search button, which is why I'm trying to see how to get the render to work with a specific number.

为什么会这样?我试过用视图记录 steamId,我看到它作为一个普通的整数传递.我唯一合乎逻辑的假设是视图加载,使用steamId"调用 API 然后呈现 steamId 但我只是在猜测?

Why could that be the case? I have tried logging steamId with the view, and I see it being passed through as a normal integer. My only logical assumption is that the view loads, calls the API with 'steamId' THEN renders steamId but I'm just guessing?

更新 Trial-n-Error

所以我尝试使用类似的东西

So I tried out using something like

<h1><%= header %></h1>
<% appCall = steamId %>
<% player = SteamWebApi::Player.new(appCall) %>
<% owned_games = player.owned_games(include_appinfo: true)%>
<%= appCall %>
<%= owned_games.success %>
<% owned_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 %>

我能够通过 puts 获得输出,并且还可以使用 API 的player.success"函数.它返回True",并把 steamId 和 app 都渲染出 ID.

I am able to get an output with the puts, and also be using the 'player.success' function of the API. Which returns "True", and puts steamId and app both render out the ID.

我还测试了在 'for' 之前打印出整个数组,并且确实注意到它为我提供了所有数据.true #<OpenStruct count=31, games=[{"appid"=>300, "name"=>"Day of Defeat: Source", "playtime_forever"=>48,

I also tested printing out the entire array before the 'for', and did notice it gives me all of the data. true #<OpenStruct count=31, games=[{"appid"=>300, "name"=>"Day of Defeat: Source", "playtime_forever"=>48,

所以问题在于部分的for"部分,我在想.我是否需要在主文档中运行 for,并从部分提供数据?好像……

So the issue lies in the 'for' section of the partial, I'm thinking. Would I need to run the for in the main document, and feed it data from the partial? That seems of...

推荐答案

问题最终是我用作模板的 Partial.它从未将 'games' 数组修改为 'game',因此当它遍历游戏 'each' 时,它从未返回任何结果.

The issue ended up being the Partial I had used as a template. It never modified the array of 'games' into 'game', so when it was iterating through the games 'each' it never returned any results.

我的最终部分按预期工作.

My final partial works as intended.

<h1><%= header %></h1>
<% player = SteamWebApi::Player.new(steamId) %>
<% owned_games = player.owned_games(include_appinfo: true)%>
<% game = owned_games.games %>
<% 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 %>

一旦我添加了额外的修饰符,它就能够成功呈现列表.

Once I added the additional modifier, it was able to render the list successfully.

这篇关于在 Rails 4.2 中将区域设置变量推送到部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆