Looping through space separated list in Rails 3 - ruby-on-rails

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

Related

Ruby is adding weird spacing between words

My website is printing out elements such as (SnO), however, it should be printing SnO, but it is adding a weird space and it is printing like Sn O. It is adding a space between the element for no reason. My code is on the listed below.
<% saved_element = ""%>
<% sensor.base_material.elests.each_with_index do |elest, v| %>
<% if elest.element.include? "O" %>
<% saved_element = elest %>
<% else %>
<%=elest.element.split('-').last %>
<% if elest.stoich != 1 %>
<sub><%=elest.stoich.to_i%></sub>
<% end %>
<% end %>
<% if v == sensor.base_material.elests.length-1 %>
<%=saved_element.element.split('-').last%>
<% if saved_element.stoich != 1 %>
<sub><%=saved_element.stoich.to_i %></sub>
<% end %>
<% end %>
<% end %>
The code you show is full of white spaces (at the beginning of each line). Those are printed on the HTML and compacted as one space. Also, when you print a value, it adds an space at the end, you can supress that usign <%= ... -%> (note the dash at the end)
https://www.howtobuildsoftware.com/index.php/how-do/Nzr/ruby-on-rails-erb-suppressing-spaces-in-erb-template
Anyway, I would move all that logic to a helper method, that's what helper methods are for.

Conditionals and overriding

I'm using a conditional to check if two values exists. I want to override the first conditional with the second one. Most likely if the second one exists then the first will, if that makes sense. Not the issue i'm having is using two different call methods.
What I have so far:
<% #data.each do |i| %>
<% if i.stock.present? %>
In Stock
<% elsif i.sold.present? %>
Sold
<% else %>
n/a
<% end %>
<% end %>
So, if in stock display 'In Stock', if sold display 'Sold'.
I think I have a solution, but feel free to let me know if there is a better way. This is using the unless statement and seems to display what I need.
<% #data.each do |i| %>
<% unless i.stock.present? %>
In Stock
<% else if i.sold.present? %>
Sold
<% else %>
n/a
<% end %>
<% end %>
<% end %>

Dynamically displaying the column values

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 "".

in_groups_of(3) but only for the first 3 groups

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 %>

Splitting string into linked words

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 %>

Resources