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%>
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)
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%>
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.
So I have standart scaffold cycle that prints a string of tags of a meet.
<% #meets.each do |meet| %>
<p class="MeetTags"><%= meet.tags %></p>
<% end %>
How can I separate string of for example 3 words, so that each would be in separate box.
Thank you!
You can do some think like
<% #meets.each do |meet| %>
<% meet_tags = meet.tags.split(' ') %>
<% meet_tags.each do |meet_tag|%>
<p class="MeetTags"><%= meet_tag %></p>
<%end%>
<% end %>
your problem is that meet.tags is an array, so you're telling rails to display the array: that's why you're seeing the square braces.
If you want to split tags into groups of 3, and then show them separated by spaces, you could do this:
<% #meets.each do |meet| %>
<% tag_arr = meet.tags.split(' ') %>
<% tag_arr.in_groups_of_3.each do |tag_subarr|%>
<p class="MeetTags"><%= tag_subarr.reject(&:blank?).join(" ") %></p>
<%end%>
<% end %>
I'm using .reject(&:blank?) because the in_groups_of method will pad the last subarray out with nil, which will mess up your formatting, and maybe cause other problems if you try and do anything with the member elements of tag_subarr.
Assuming that your tags are joined with space:
<% #meets.each do |meet| %>
<% meet.tags.split(' ').each do |word| %>
<p class="MeetTags"><%= word %></p>
<% end %>
<% 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 %>