Ruby each, different action depending on index, with a pattern? - ruby

Ok so my title may be confusing. What I want to do is to loop through a collection of models, and for the first two, render a template, for the next four render a different template and next two render the same template as for the first ones, and so on.
Like this:
<% ads.each do |ad| %>
<% # if it's 1-2, 7-8, 13-14 and so on render as big' %>
<%= render 'front/home/big_ad', ad: ad %>
<% # if it's 3-6, 9-12, 15-18 and so on render as small' %>
<%= render 'front/home/small_ad', ad: ad %>
<% end %>
<% end %>
What would be the cleanest way to accomplish this?

If your groups would be even long, then you could use in_groups_of command, and alternate between them, but with these specifications, the easiest way is this:
<% ads.each_with_index do |ad, index| %>
<% if (index % 6 < 2) %>
<%= render 'front/home/big_ad', ad: ad %>
<% else %>
<%= render 'front/home/small_ad', ad: ad %>
<% end %>
<% end %>

Related

How to list(<li>) data from a column in a table having newlines(\r +) inside ruby tag?

My code is
#tariff.each do |tar|
<li><%= tar.lt_ninety%></li>
<%end%>
This lt_ninety has a value (hey hello) contain new line(\r +) in between each word in table.
I have found one method in Stack Overflow, but couldn't help me to solve.
<li><%= h(tar.lt_ninety).gsub(/\r/, '<br/>').html_safe%></li>
and output was like
<li> hey</li>
hello
I cannot wrap hello in <li>
Split up tar.lt_ninety and loop it
<% #tariff.each do |tar| %>
<% tar.lt_ninety.split(/\r/) do |ln|%>
<li><%= h ln %></li>
<% end %>
<%end%>
Just split tar.lt_ninety into separate lines before generating a <li> for each line:
<% #tariff.each do |tar| %>
<% tar.lt_ninety.lines.each do |line| %>
<li><%= line %></li>
<% end %>
<%end%>
Hurray!! my colleague gave me a cool solution
<ul><% (tar.lt_ninety).split(/\r\n/).each do |val| %>
<li><%= val %></li>
<% end%></ul>

Make counter variable in each method increase itself

How to make a variable within .each method increasable, so the output would be 1, 2, 3...Instead of this:
<% #all_posts.each do |p, a = 0| %>
<% a += 1 %>
<%= a %>
<% end %>
Output: 1, 1, 1...
Just use each_with_index instead of each
Only caveat: the index starts with 0.
<% #all_posts.each_with_index do |p, a| %>
<%= a %>
<%= p.name %>
<% end %>
See http://ruby-doc.org/core-2.3.0/Enumerable.html#method-i-each_with_index
To make it accessible, initialize it outside of the loop.
<% a = 0 %>
<% #all_posts.each do |p| %>
<% a += 1 %>
<%= a %>
<% end %>
But a better way is:
<% #all_posts.each.with_index(1) do |p, a| %>
<%= a %>
<% end %>

Use a variable name in an erb include call

I'm using Sinatra and I'd like to include another erb file in one of my views but using a variable for the file name.
I was thinking something like this may work but I get an error saying no such file could be found.
<%= erb :'layout/nav/#{device_type}' %>
Currently I'm using the below switch statement to achieve the desired results but the above would be cleaner and less code.
<% case device_type
when 'mobile'%>
<%= erb :'layout/nav/mobile' %>
<% when 'tablet' %>
<%= erb :'layout/nav/tablet' %>
<% else %>
<%= erb :'layout/nav/desktop' %>
<% end %>
Thanks
Giles
I think that could work, you just need to use double quotes to interpolate strings.
Try
<%= erb :"layout/nav/#{device_type}" %>

erb idiom to handle undefined variable

I'm trying to write some puppet .erb, I'd like to handle this "environment" variable if it's:
undefined
a string with newlines
an array.
I've got as far as this:
<% Array(environment).join("\n").split(%r{\n}).each do |f| %>
one line: <%= f %>
<% end %>
But haven't gotten around the undefined case yet. I've tried this
<% if (defined?(environment)).nil? %?
<% Array(environment).join("\n").split(%r{\n}).each do |f| %>
one line: <%= f %>
<% end %>
<% end %>
but am still getting "(erb):11: undefined local variable or method `environment' for main:Object (NameError)" when trying to test it like this:
ruby -rerb -e "environmentUNDEFINEME= [ 'cronvar=cronval', 'var2=val2' ];
puts ERB.new(File.read('templates/job.erb')).result"
Sorry this is so basic, but somebody's got to ask the easy questions. Any help?
I would do this:
<% if defined?(environment) %>
<% Array(environment).each do |f| %>
one line: <%= f %>
<% end %>
<% end %>
I didn't understand why you joining on new lines and then splitting on them again, so I removed it from the example.

How to create a sequence of numbers in a Ruby on Rails form

I have 10 form fields and I want to append a number next to each field, going sequentially from 1 to 10. The problem is that the code is already in a loop. It's inside a partial, and the partial is passed a collection.
<%= fields_for "list", f do |f| %>
<!-- I want 1, 2, etc to appear here depending on the iteration. -->
<%= f.label :name %>
I tried using <%= i += 1 %> but it does not work since i is not defined. If I define i, it will keep getting reset to the same number, so it makes no difference. Any ideas?
Rails automatically defines a local variable called partialname_counter where, obviously, "partialname" is the name of your partial. So if your partial is called e.g. _list_item.html.erb you could write it like this:
<%= fields_for "list", f do |f| %>
<%= list_item_counter + 1 %>
<%= f.label :name %>
<% end %>
(The + 1, of course, is there because the counter starts at 0.)
Another option would be to just let the browser do the numbering for you using an ordered list:
In the view:
<ol>
<%= render :list_item, :collection => #some_items %>
</ol>
...and in the partial:
<%= fields_for "list", f do |f| %>
<li>
<%= f.label :name %>
</li>
<% end %>
This option is probably more semantic, and lists are easy to style in CSS.

Resources