Expanding on New row every 3 items -- I'm trying to insert ads in between my Forem (https://github.com/radar/forem) topics -- one in between every 3 topics for the first 3 groups of topics (3 ads in total).
UPDATE
I ended up with this thanks to the answer below, unfortunately it doesn't seem to pass on test as a valid local (can't find topic inside forem/topics/topic):
<% #topics.in_groups_of(3).each_with_index do |grouped_topics, index| %>
<%= render partial: "forem/topics/topic", collection: grouped_topics %>
<% if index < 3 %>
<p>Ad</p>
<% end %>
<% end %>
Live test app (click the big green Run button to test):
http://runnable.com/VFUNK2ho3Fpr8Fp2/forem-with-ads-in-between-topics
File in question: views/forem/forums/show.html.erb
in_groups_of split your array into array of arrays, so that should be:
<% #comments.in_groups_of(3, false).each_with_index do |grouped_comments, index| %>
<% grouped_comments.each do |comment %>
...
<% end %>
<% if index < 3 %>
<%= image_tag "selfie.jpg">
<% end %>
<% end %>
Related
I have 100 shoes with shoe names. I don't want to display all 100 in a row, I want to display 5, then a green box, then the next 5, and the same green box... but there is something wrong with my code.
<% #shoes.each.with_index(1) do |shoe, index| %>
<% while index < 101 do %>
<%= shoe.name %>
<% if index % 5 == 0 %>
<%= Green Box %>
<% end %>
You're looking for in_groups_of(x)
<% #shoes.in_groups_of(5) do |shoe_groups| %>
<% shoe_groups.each do |shoe| %>
<%= shoe.name %>
<% end %>
<%= 'Green Box' if shoe_groups.size % 5 == 0 %>
<% end %>
Your syntax is wrong in several places - the enumerator is wrong, you are missing several end statements. Also, even though index is not a reserved word, the generally accepted style for an index is a single-letter variable like i. It should be
<% #shoes.each_with_index(1) do |shoe, i| %>
<% while i < 101 do %>
<%= shoe.name %>
<% if i % 5 == 0 %>
<%= Green Box %>
<% end %>
<% end %>
<% end %>
(But personally, I would not do the index < 101 block in the view - I would make sure that the controller that generates #shoes and sends it to the view only sends 100 elements in the array)
Here, I have 10 columns i.e., answer1, answer2, answer3, ..., answer10 in the table MgAnswer.
I have to check whether each column value is present or not. Only if it present,then I have to display it in the page.
Im giving column names dynamically within for loop
<% (1..10).each do |i| %>
<% if MgAnswer."answer#{i}".present? %>
<%= MgAnswer."answer#{i}" %>
<% end %>
<% end %>
Im ending up with Syntax error.
You can indeed dynamically invoke methods in ruby, but this is not the syntax. Instead do
<% (1..10).each do |i| %>
<% if MgAnswer.public_send("answer#{i}").present? %>
<%= MgAnswer.public_send("answer#{i}") %>
<% end %>
<% end %>
It should seem like the following:
<% (1..10).each do |i| %>
<%= MgAnswer.send("answer#{i}") %>
<% end %>
Since ruby can't evaluate line as MgAnswer."method". Also you can just skip if condition, because it will be evaluated to empty string "".
Rails 4
I have a 15 posts being shown in array list <% #posts.each do |post| %>. I'd like to insert my ads image after the first 3 posts only. NOT every 3 posts. How can I achieve that?
been looking at this http://ruby-doc.org/core-2.2.2/Enumerable.html but don't know which one is suitable for my question.
You can try this:
<% #posts.each_with_index do |post,i|%>
<%if i == 2 %>
#your image here
<%end%>
<%end%>
Try using each with index
<%#posts.each.with_index(1) do |post, index| %>
<% if index==3 %>
your code to insert image
<%end%>
Try This
<% ADD_AFTER = [3,7,8,15]%>
<% #posts.each_with_index do |post, index| %>
<%if ADDS_AFTER.include?(index)%>
#MY FAV ADD CONTENT
<%end%>
#Regular code
<%end%>
Give Line Number in ADD_AFTER and it will print your advertisement after the desired line nujmber
After the third post it will place an add
`<% #posts.each_with_index do |post,index| %>
<% if index == 3 %>
<%= place_add_here %>
<% end %>
<% end %>`
Since the other methods listed are not working. This is one way to achieve it.
<% counter = 0 %>
<% #posts.each do |post|%>
<%if counter == 2 %>
#your image here
<%end%>
<% counter++ %>
<%end%>
New to Rails! I have products which are associated to a campaign. Each #product has a .price and .orders_count associated to it.
What I wanted to do is multiply the .price with .orders_count for each #product, and add them all up to get a total cost for the #campaign.
Being new to Rails, wasn't sure how to do the write syntax and had the following in the view. This does the first piece, but does not add them all up at the end. Thanks for the help!
<% #products.each do |p| %>
<% if p.orders_count? %>
<%= (number_to_currency((p.price) * p.orders_count)) %>
<% end %>
<% end %>
Try setting a variable to keep track of the sum of all products, and add the product to it each time through the loop. Try this:
<% sum = 0 %>
<% #products.each do |p| %>
<% if p.orders_count? %>
<% product = (number_to_currency((p.price) * p.orders_count)) %>
<% sum += product %>
<%= product %>
<% end %>
<% end %>
The sum is: <%= sum %>
I am using Rails 3.
I got a tags in a column in the database that is saved with space separating each of them like so:
apple orange banana
I want to loop over them and put each on a separate line (between P tags). I got it somewhat running using the code below but the last line outputs the entire string too.
<p>apple</p>
<p>orange</p>
<p>banana</p>
apple orange banana (I do not want this line)
The code I use is this, how can I make it better / replace it so that I do not get the last line when I output the post?
<% item.options.each(' ') do |item| %>
<p><%= item %></p>
<% end %>
<% item.options.split(" ").each do |item| %>
<p><%= item %>
<% end %>
or better yet you might create a virtual attribute in your model:
def tags do
self.options.split(" ")
end
and then
<% item.tags.each do |tag| %>
<p><%= tag %>
<% end %>
<% (item.options.split(' ')).each do |item| %>
<p><%= item %></p>
<% end %>
that should work well