Infinite scroll with group_by items - ruby-on-rails

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.

Related

Rails 5: Nil error in the first argument of a form

I'm quite new with rails so excuse my ignorance.
I have an homepage with a view of astronomic observations and I want to put in the same page the view of the outings that the observatory organize.
Since I can't append two views in the same page I tryed to pass the second view as a partial but I get an error
First argument in form cannot contain nil or be empty
So I though it was a problem of variables and tryed to pass the local variable of the partial but nothing changed.
So how do I pass a variable of #outing to the partial outings/_index.html.erb which will be visualized in the view of observative_sessions
I defined #outing=Outing.new in outings_controller which is the controller for the view outings
In app > views > observative_sessions I have the index.html.erb with
<div class="panel panel-default" id = "observative_sessions">
...
</div>
<%= render 'outings/index', :outings => #outing %>
In app > views > outings I put an _index.html.erb
<%= render 'outings/new_modal' %>
<div class="panel panel-default">
...
<tbody>
<% #outings.each do |outing| %>
...
<% end %>
</tbody>
The _new_modal.html.erb calls the _form.html.erb
<%= render 'outings/form' %>
And then the form
<%= bootstrap_form_for #outing do |f| %>
<%= f.date_field :day, label: 'Giorno:' %>
<%= f.text_field :location, label: 'Luogo:' %>
<%= f.time_field :time, label: 'Ora:' %>
<%= f.submit 'Aggiorna' %>
<% end %>
I get the error in the first line of the form with #outing.
If I put the view in a page alone everything work but not with the partial.
So what did I do wrong?
Thank you in advance for all the help.
In app > views > observative_sessions I have the index.html.erb with
<div class="panel panel-default" id = "observative_sessions">
...
</div>
<%= render partial: 'outings/index', locals: {outing: #outing} %>
In app > views > outings I put an _index.html.erb
<%= render partial: 'outings/new_modal',locals: {outing: outing} %>
<div class="panel panel-default">
...
<tbody>
<% #outings.each do |outing| %>
...
<% end %>
</tbody>
The _new_modal.html.erb calls the _form.html.erb
<%= render partial: 'outings/form', locals: {outing: outing} %>
Then in your form use the local outing variable:
<%= bootstrap_form_for outing do |f| %>
<%= f.date_field :day, label: 'Giorno:' %>
<%= f.text_field :location, label: 'Luogo:' %>
<%= f.time_field :time, label: 'Ora:' %>
<%= f.submit 'Aggiorna' %>
<% end %>
You need to pass #outing variable to all forms those are using #outing variable.
Change the line of rendering _new_modal.html.erb partial like this
<%= render 'outings/new_modal', outing: outings %>
Then change the line of rendering _form.html.erb like this
<%= render 'outings/form', outing: outing %>
And finally change the actual form like this
<%= bootstrap_form_for outing do |f| %>
<%= f.date_field :day, label: 'Giorno:' %>
<%= f.text_field :location, label: 'Luogo:' %>
<%= f.time_field :time, label: 'Ora:' %>
<%= f.submit 'Aggiorna' %>
<% end %>

Rails :How to move code from view to partial file

In edit.html.erb
<% if material.look %>
<%= form_for material do |f|%>
<% if material.approval = "1" %>
<%= f.hidden_field :date,value: Date.today %>
<% end %>
<%= f.hidden_field :approval_amount, value: 0 %>
<%= f.hidden_field :approval_amount_percentage, value: 0 %>
<%= f.submit 'yes' %>
<% end %>
<%= form_for material do |f|%>
<%= f.hidden_field :approval_amount, value: 0 %>
<%= f.hidden_field :approval_amount_percentage, value: 0 %>
<%= f.submit 'no' %>
<% end %>
<% end %>
Code in two forms are duplicate, so I want to move them to partial file,
In _material_form.html.erb I code like this
<%= f.hidden_field :approval_amount, value: 0 %>
<%= f.hidden_field :approval_amount_percentage, value: 0 %>
In edit.html.erb, I modifies duplicate code
<% if material.look %>
<%= form_for material do |f|%>
<% if material.approval = "1" %>
<%= f.hidden_field :date,value: Date.today %>
<% end %>
<%= render 'material_form'%>
<%= f.submit 'yes' %>
<% end %>
<%= form_for material do |f|%>
<%= render 'material_form'%>
<%= f.submit 'no' %>
<% end %>
<% end %>
However, it shows an error
undefined local variable or method `f' for
What causes the error and how to fix it ? Thanks.
Your form, assigned to the variable f is out of scope in your partial. To use it, you'll need to pass f in as a local value to your partial:
<%= render 'material_form', f: f%>
The rails documentation on partials is quite helpful; you can also dig into the action renderer documentation if you want to go deeper.

Pushing a Locale Variable to Partial in Rails 4.2

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.

Couldn't save collection_radio_buttons in array Ruby on Rails

We have a problem to save the values of the radio_buttons of a loop. It doesn't save in an array.
The SavedAnswer model has an has_and_belongs_to_many relation with the MultipleChoiceAnswer model.
This is my code:
<%= form_for #saved_answer do |f| %>
<% #questions.each do |question| %>
<%= collection_radio_buttons(:saved_answer, :multiple_choice_answer_ids , question.multiple_choice_answers, :id, :title) do |c| %>
<%= c.radio_button %>
<%= c.label %>
<% end %>
<% end %>
<%= f.submit "Submit" %>
<% end %>
My output is
Parameters: {"utf8"=>"✓", "authenticity_token"=>"57I9yLZMccvcb3Bn5/pw7kES0c9CUAGs33yCXoS0Urm1Yek/Baz8Hl7fO8Yl/OVZWLKsX7qrwOlqEBoXrGkcxQ==", "saved_answer"=>{ "multiple_choice_answer_ids"=>"1"}, "commit"=>"Submit"}
Thanks in advance!
You have form_for with |f| and collection with |f|.
It's not a good decision, i think. That can cause problems.
If not - write what you receive in params when trying to save.
Just use check boxes for your issue:
<%= form_for #saved_answer do |f| %>
<% #questions.each do |question| %>
<%= check_box_tag "saved_answer[multiple_choice_answer_ids][]", question.id, #saved_answer.multiple_choice_answer_ids.include?(question.id) %>
<%= question.title %>
<% end %>
<%= f.submit "Submit" %>
<% end %>
Refer: http://railscasts.com/episodes/17-habtm-checkboxes?view=asciicast

Render form partial from view ROR

Im trying to render a form from another controller in my view.
This is posts_index, and the render post.comments works fine, but the form for a new comment doesnt.
<% #posts.each do |post| %>
<%= link_to post.title, post %>
<%= simple_format post.text %>
<%= render post.comments.order('created_at DESC').all %>
<%= render :partial => '/comments/form', locals: {post: post} %>
I get this error: undefined method `comments' for nil:NilClass
The comments form:
<%= form_for([#post, #post.comments.build]) do |f| %>
<%= f.label :Comment %><br />
<%= f.text_area :body, :class => "comment_text_box" %>
<%= f.submit %>
<% end %>
I understand I need to pass the post.comments to the form, but can't figure out how.
Post_show works with <%= render "comments/form" %>
Post and comments are a has_many relationship, posts has_many comments.
Thanks for looking.
You need to pass variables into your partial like this:
<% #posts.each do |post| %>
<%= link_to post.title, post %>
<%= simple_format post.text %>
<%= render post.comments.order('created_at DESC').all %>
<%= render partial: '/comments/form', locals: {post: post} %>
<% end %>
and change your form partial to this:
<%= form_for([post, post.comments.build]) do |f| %>
// form fields
<% end %>
When you ask for the partial, you need to send it the post it's related to. It would look like this:
<% #posts.each do |post| %>
<%= link_to post.title, post %>
<%= simple_format post.text %>
<%= render post.comments.order('created_at DESC').all %>
<%= render :partial => '/comments/form', post: post%>

Resources