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%>
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)
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 %>
I want to print out a list of links separated by commas in Rails.
Heres what I've got:
<%= topics.each do |topic| %>
<a href="<%= topic.link %>" ><%= topic.name %></a>
,
<% end %>
Heres what I want:
Thing A,
Thing B,
Thing C
But right now I get an extra comma on the last iteration of the loop! What should I do?
One way of doing this is with map then Array#join:
<%= topics.map { |topic| link_to(topic.name, topic.link) }.join(',').html_safe %>
if you want to do minimum possible change to your code, you can use the following
<%= topics.each do |topic| %>
<a href="<%= topic.link %>" ><%= topic.name %></a>
<% if(topic != topics.last) %>
,
<% end %>
<% end %>
How about using each_with_index, and only put comma before the content unless it's not the first item.
<% topics.each_with_index do |topic, i| %>
<% if i > 0 %>
,
<% end %>
<%= topic.name %>
<% end %>
I made it in one line call (for active records collections) using the concat helper:
<% concat (',') if e.bills.last != b %>
concat is an ERB helper (TextHelper) to add some HTML without the <%= %> syntax, helpful to add few characters.
Here is the full code to make it clear:
<% event.bills.each do |b| %>
<%= link_to(b.number.to_s, bill_display_path(b)) %>
<% concat (',') if e.bills.last != b %>
<% end %>
Simply try this. It works for me
<%= topics.map{|p| p.topic.name}.join(",") %>
You can do the following to print out the comma for all items except for the last:
<% topics.each do |topic| %>
<%= topic %>
<%= "," if topic != topics.last %>
<% end %>
This will check if the current item in the loop is the last item, and will use the <%= %> syntax to output the comma.
In code:
<% #offer2.each do |offer|%>
<% #menuName=Menu.find_by_menu_item_name(offer.menuName_get)%>
<% #line_item.each do |line_item|%>
<%#validPrice=(#menuName.price)*(offer.disAmountOrPercentage)%>
<%end%>
<%end%>
I am new in rails.I want to add #validPrice += #validPrice but it's not working inside the loop or outside the loop.So how do I get the sum of this variable.
Try this:
<% #validPrice = 0 %> #initialized with zero
<% #offer2.each do |offer|%> # 1st loop
<% #menuName=Menu.find_by_menu_item_name(offer.menuName_get)%>
<% #line_item.each do |line_item|%> # 2nd loop
<% #validPrice += (#menuName.price)*(offer.disAmountOrPercentage) %>
<%end%>
<%= #validPrice %> # you can access and print #validprice out of 2nd loop here
<%end%>
You should declare #validPrice= 0 on the top of the loop. So that it will not initialize repeatedly.
<% validPrice = 0 %>
<% #offer2.each do |offer|%>
<% #menuName=Menu.find_by_menu_item_name(offer.menuName_get)%>
<% #line_item.each do |line_item|%>
<% validPrice += (#menuName.price)*(offer.disAmountOrPercentage) %>
<%end%>
<%end%>
It will be better if you would make model method for this summations, rather to run in view pages.
Try this
<% #offer2.each do |offer|%>
<% #menuName=Menu.find_by_menu_item_name(offer.menuName_get)%>
<% #line_item.each do |line_item|%>
<%= validPrice = (#menuName.price)*(offer.disAmountOrPercentage) %>
<%end%>
<%end%>
When you go to blog page, you will see the archive list on the menu.
In most cases, it shows something like this
'Archive'
2012(78)
-December(1)
-November(5)
-October(10)
...
2011(215)
2010(365)
I'm confident to make blog posting system by using scaffold.
But I have no idea how to make this Archive:(
Anyone come up with good idea to implement this to app easily???
Need your help!!
<h3>Archives </h3>
<% if #posts.to_a.empty? %>
<div class="post">
<p>No articles found...</p>
</div>
<% else %>
<% current_month = 0 %>
<% current_year = 0 %>
<% for article in #posts %>
<% if (article.created_at.year != current_year)
current_year = article.created_at.year
%>
<h3 class="archiveyear"><%= article.created_at.year%></h3>
<% end %>
<% if (article.created_at.month != current_month || article.created_at.year != current_year)
current_month = article.created_at.month
current_year = article.created_at.year
%>
<h4 class="archivemonth"><%= (Date::MONTHNAMES[article.created_at.month]) %></h4>
<% end %>
<div class="archivepost">
<%= link_to article.title, article_path(article), :remote => true %> on <%= article.created_at.strftime('%A')%> - <%= article.created_at.strftime('%d') + "th"%>
</div>
<% end -%>
<%end %>
This may help you. I ve not included the number of counts in this code. Actually m figuring how to do it. If u can let me know.
Also in the controller ive done this.
#posts = Article.order("created_at DESC")
The #posts is an array so the items inside it ll be ordered and then i can fetch the records according to it ordering.
Thanks.