Pushing a Locale Variable to Partial in Rails 4.2 - ruby-on-rails

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 %>
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) When you check someones profile, you see their list.
B) You can search with a users ID, to pull the query results.
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 } %>
And the "Welcome to His Page!" is getting passed, but I'm having a lot of issues getting :steamId to render.
In the partial view I've tried rendering it with <%= steamId %> <% steamId %> and :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.
I'm welcome to any input.
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?
Thanks for any input!
EDIT:
Additional Info:
The issue with this was partially in the partial.
<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 %>
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.
Resolved code:
<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 %>
However, I am only able to have it render if it pulls the player_steam_id from the DB.
So for instance if I have
:steamId => 76561198017108025
It won't work, but if I have
:steamId => User.player_steam_id it does work.
(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.
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?
Update 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 %>
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.
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,
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...

Is game_list a partial or is it a regular view? A partial will begin with an underscore as the file name. _game_list.html.erb
Try this
<%= render 'game_list', :header => "Welcome to His Page!", :steamId => XXXXXXXXXX %>
Then you can render steam ID like this <%= steamId %> inside of the partial.
Edit:
If you are not referencing steamId in direct output area you don't need to wrap it in <%= %>
Try SteamWebApi::Player.‌​new(steamId) instead of SteamWebApi::Player.‌​new(<%= steamId %>)
Another Edit
Try this
<h1><%= header %></h1>
<% 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 %>
I added
This is my steam ID <%= steamId %>

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.

Related

Infinite scroll with group_by items

I'm trying to implement infinite scroll on a list of items (aka "Games" in my code) following this tutorial --> http://railsforbeginners.com/chapters/chapter-9-infinite-scroll/
Here is my view :
<% #game_days.each do |day, games| %>
<% for game in games %>
<%= game.game_external_link %>
<%= image_tag game.photos[0].image.url(:thumb), class:"img-responsive" if game.photos.length > 0 %>
<%= game.game_name %>
<%= game.game_description %>
<%= image_tag avatar_url(game.user) %>
<% end %>
<% end %>
I've tried to refacto the code putting the code between <%= game.game_external_link %> to <%= image_tag avatar_url(game.user) %> in a partial and then putting this <%= render #games %>. But i get an infinite loop now :(
So I need help to refacto and using partial (as it says in the article).
Cheers!
# app/vies/games/_game.html.erb
<%= game.game_external_link %>
<%= image_tag game.photos[0].image.url(:thumb), class:"img-responsive" if game.photos.length > 0 %>
<%= game.game_name %>
<%= game.game_description %>
<%= image_tag avatar_url(game.user) %>
You can then render the whole collection with just:
<%= render games %>
This is shorthand for:
<%= render partial: 'game', collection: games %>
See the rails guides for how this works.

Unable to get the value of a variable inside Rails erb tags

I want to understand the erb usage. In the below code I am unable to figure out how to get the value of (group.id) in the if clause using the erb tags.
This probably has a very basic solution but I am unable to get proper answers.
The below code gives me syntax error.
<% current_user.favorite_groups.to_a.each do |group| %>
<%= if (group.id).newfavorite_texts.exists?(id: text.id) %>
<%= group.name %>
<%= link_to # do something %>
<% else %>
<%= group.name %>
<%= link_to # do something else %>
<% end %>
<% end %>
Thanks in advance.
You should be get going with the below code
<% current_user.favorite_groups.to_a.each do |group| %>
<% if group.newfavorite_texts.exists?(id: text.id) %>
<%= group.name %>
<%= link_to # do something %>
<% else %>
<%= group.name %>
<%= link_to # do something else %>
<% end %>
<% end %>

Rails loop in html erb

How can I simplify the following lines:
<% if #campaign.previous_campaign.present? %>
<%= #campaign.previous_campaign.product_name %>
<% if #campaign.previous_campaign.previous_campaign.present? %>
<%= #campaign.previous_campaign.previous_campaign.product_name %>
<% end %>
<% end %>
I need to keep adding ".previous_campaign" until it is not present. So the next one in the above code would be:
<%= #campaign.previous_campaign.previous_campaign.previous_campaign.product_name %>
etc etc.
Something like this:
<% campaign = #campaign %>
<% while campaign.previous_campaign.present? %>
<% campaign = campaign.previous_campaign %>
<%= campaign.product_name %>
<% end %>
The code may need some debugging, but I guess the idea is clear
You could do something like this:
<% for c in #campaign do %>
<% if c.previous_campaign.present? %>
<%= c.previous_campaign.product_name %>
<% end %>
<% end %>

How do I separate elements by their type with an each method?

I created two scaffolds: announce_sections and announcements. The announce_sections are the types of announcements there are (i.e. games, tryouts, etc) and when I create an announcement I specify what type of announce_sections it is. I'm trying to display it so that each announce_section is viewed, with each announcement and its information under the announce_section. This is what I came up with:
<% #announce_sections.each do |announce_section| %>
<%= announce_section.name %>
<% #announcements.each do |announcement| %>
<%= announcement.announcement_title %>
<%= announcement.information %>
<%= announcement.additional_information %>
<%= announcement.type %>
<% end %>
<% end %>
However, this code only displays the announce_sections with the all announcements under it. The announcements don't get separated into their respective announce_sections. How do I change it so that it does?
<% #announce_sections.each do |announce_section| %>
<%= announce_section.name %>
<% #announcements.where(type: announce_section).each do |announcement| %>
<%= announcement.announcement_title %>
<%= announcement.information %>
<%= announcement.additional_information %>
<%= announcement.type %>
<% end %>
<% end %>
Use the name of the field you are using to assign the announcement type instead of 'type'
There are many ways to solve this, but one simple one is to build a hash where the key is the type of announcement_section and the value is an array (or Set) of the announcement. One way to build that hash is to use the Hash.new {|hash, key| ... } form of the constructor.
#hash = Hash.new {|hash, section| hash[section] = Array.new }
#announcements.each do |a|
# for each announcment append it to the array under the hash
#hash[a.section] << a
end
And then, in the view
<% #hash.keys.each do |section| %>
<%= section %>
<% #hash[section].each do |announcement| %>
<%= announcement.announcement_title %>
<%= announcement.information %>
<%= announcement.additional_information %>
<%= announcement.type %>
<% end %>
<% end %>

Rendering a partial inside a block

I'm new to rails. I have this block in my view afrikaans.html.erb
<% #afrikaans.each do |course| %>
<div class="course">
<h3 class="course-name"><%= link_to course.name, course.url %></h3>
<% if I18n.locale == I18n.default_locale %>
<p class="course-description_en"><%= course.description_en %></p>
<% else %>
<p class="course-description_se"><%= course.description_se %></p>
<% end %>
<% if course.youtube_url.blank? == false %>
<p><%= raw ApplicationHelper.youtube_embed(course.youtube_url) %></p>
<% end %>
<% if course.language_id == 1 %>
<p> <%= image_tag("eng.png", :alt => "England", :class =>"round") %></p>
<% else %>
<p> <%= image_tag("swe.png", :alt => "Sweden", :class =>"round") %></p>
<% end %>
<% if ApplicationHelper.active_link?(course.url) == false %>
<td><%= I18n.t('home.broken_link') %></td>
<% end %>
<p><%= course.nbr_of_votes %> <%= I18n.t('home.votes') %></p>
</tr>
<% end %>
I also have another file swahili.html.erb with the same structure. I wanted to implement something like this
afrikaans.html.erb
<% #afrikaans.each do |course| %>
<%= render 'shared/partial' %>
<% end %>
So that I can also have
swahili.html.erb
<% #swahili.each do |course| %>
<%= render 'shared/partial' %>
<% end %>
The partial will contain the part of the block. I've tried this but it's not working. My question is this even possible in rails and if so what could be the problem. What options do I have if it isn't possible so that I can avoid the repetition since the two files have the same structure?
Update. This One worked out for me. I only needed to add :course => course on the block so that my views becomes something like
<% #afrikaans.each do |course| %>
<%= render 'shared/course_body', :course => course %>
<% end %>
Of course I've not named my partial "partial". This was just a matter of asking. Thanks to one #Alexander Panasyuk's answer.
Just create shared directory within your app/views path. And create file _partial.html.erb inside shared:
<div class="course">
<h3 class="course-name"><%= link_to course.name, course.url %></h3>
<% if I18n.locale == I18n.default_locale %>
<p class="course-description_en"><%= course.description_en %></p>
<% else %>
<p class="course-description_se"><%= course.description_se %></p>
<% end %>
<% if course.youtube_url.blank? == false %>
<p><%= raw ApplicationHelper.youtube_embed(course.youtube_url) %></p>
<% end %>
<% if course.language_id == 1 %>
<p> <%= image_tag("eng.png", :alt => "England", :class =>"round") %></p>
<% else %>
<p> <%= image_tag("swe.png", :alt => "Sweden", :class =>"round") %></p>
<% end %>
<% if ApplicationHelper.active_link?(course.url) == false %>
<td><%= I18n.t('home.broken_link') %></td>
<% end %>
<p><%= course.nbr_of_votes %> <%= I18n.t('home.votes') %></p>
</tr>
Then render your partial in afrikaans.html.erb like that:
<% #afrikaans.each do |course| %>
<%= render 'shared/partial', :course => course %>
<% end %>
or in swahili.html.erb:
<% #swahili.each do |course| %>
<%= render 'shared/partial', :course => course %>
<% end %>
It is most definitely possible, and usually a good idea.
In the future it would be nice if you could post the actual results and/or error messages you get, which would help a lot when trying to help you.
That said, I'm guessing you need to pass the course variable to your partial. Change
<%= render 'shared/partial' %>
to
<%= render 'shared/partial', :course => course %>
Partials do not have access to local variables in other partials. In that sense you can think of each partial as separate methods on the same object instance.
<%= render 'shared/partial', locale: 'swahili', course: course %>
You will have local vars 'locale' and 'course' in your partial set to 'swahili' and #course, respectively. Also, I'd advise to name your partials something more meaningful, like 'course'.

Resources