There is a space between the elements rails? - ruby-on-rails

As you can see below, I am having a problem of getting extra spaces in my search result. As you see in the photo, between the numbers and the elements, I am getting spaces and I do not why?
<td>
<% 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 %>
<% end %>
<% if saved_element.present? %>
<%= saved_element.element.split('-').last%>
<% if saved_element.stoich != 1 %>
<sub><%= saved_element.stoich.to_i %></sub>
<% end %>
<% end %>
</td>

Related

Output sum of decimal/float array rails

I have the following which i thought would get me the sum of the array but doesnt:
<% #orders.each do |order| %>
<% if Product.exists?(sku: order.line_items.where().map {|li| li.sku }) %>
<%= order.line_items.where(vendor_name: #vendor.vendor_name).map do |li| %>
<% if Product.exists?(sku: li.sku) %>
<% product = Product.find_by(sku: li.sku ) %>
<% ((li.store_price.to_d * li.store_fulfillable_quantity) - (product.production_price * li.store_fulfillable_quantity)) * (0.70) %>
<% end %>
<% end.compact.sum %>
<% end %>
<% end %>
The output is something like:
25.21 25.21 12.66 5.33 12.66 9.01
I need to add these numbers. How can I do this with the decimals/floats?
When I use
<%= #orders.each do |order| %>
I get undefined method + for nilclass for the line that is on, and it's not nil but obviously I'm missing some factor on how sum works.
<%= #orders.map do |order| %>
<% if Product.exists?(sku: order.line_items.where().map {|li| li.sku }) %>
<% order.line_items.where(vendor_name: #vendor.vendor_name).map do |li| %>
<% if Product.exists?(sku: li.sku) %>
<% product = Product.find_by(sku: li.sku ) %>
<% ((li.store_price.to_d * li.store_fulfillable_quantity) - (product.production_price * li.store_fulfillable_quantity)) * (0.70) %>
<% end %>
<% end.compact.sum %>
<% end %>
<% end.compact.sum %>
Is how i got it to work
method reduce in arrays designed for getting reduced value of collection:
array.compact.reduce(:+)
# => Sum of array
more info about reduce

How to get previous item in each loop

I have a SQL query that loads information into a hash. I want to iterate through that information and output it into the view.
If the user_id is the same as the previous object's user_id, I don't want to display that user_id, just the name and everything else. It seems like the logic should be simple, but being a novice at Ruby and Rails, I'm not sure what is really available to do this.
I was trying something like this, but prev_id was never getting updated after the first iteration:
<% #session.each_with_index do |s, x| %>
<% if x == 0 then prev_id = 'nil' end %>
<% curr_id = s['id'] %>
<% if curr_id != prev_id %>
<%= s['id'] %>
<% end %>
<%= s['name'] %>
<%= s['count'] %><br>
<% prev_id = curr_id %>
<% end %>
Any help is greatly appreciated!
You have to declare your prev_id outside of the loop for it to persist between iterations:
<% prev_id = nil %>
<% #session.each_with_index do |s, x| %>
<% if x == 0 then prev_id = 'nil' end %>
<% curr_id = s['id'] %>
<% if curr_id != prev_id %>
<%= s['id'] %>
<% end %>
<%= s['name'] %>
<%= s['count'] %><br>
<% prev_id = curr_id %>
<% end %>
You should use chunk.
<% #session.chunk{|s| s["id"]}.each do |curr_id, a| %>
<%= curr_id %>
<% a.each do |s| %>
<%= s["name"] %>
<%= s["count"] %><br>
<% end %>
<% end %>

undefined method `author' for nil:NilClass

I have some problems with my rails application.
<% #story.chapters.each do |chapter| %>
<% if round.to_s != chapter.round %>
<% if chosenChapterRound != chapter.round %>
<% maxVotes = 0 %>
<% end %>
<% if chapter.votes.count > maxVotes %>
<% maxVotes = chapter.votes.count %>
<% chosenChapterRound = chapter.round %>
<% chosenChapters[round.to_i] = chapter %>
<% end %>
<% end %>
<% end %>
<% chosenChapters.each do |chosenChapter| %>
<%= chosenChapter %>
<% end %>
this outputs something like this: #< Chapter:0x007fa1b8f59888>
What I really want to do is this:
<% chosenChapters.each do |chosenChapter| %>
<%= chosenChapter.author %>
<% end %>
But whenever I try to access one of the attributes inside of chosenChapter, I get a the following errormessage:
undefined method `author' for nil:NilClass
What am I doing wrong? Thanks!
At least one of your chosenChapters is nil. The problem is probably in the following fragment:
<% maxVotes = chapter.votes.count %>
<% chosenChapterRound = chapter.round %>
<% chosenChapters[round.to_i] = chapter %>

If Condition in each do Rails

Hi i need to print out just the candidates where active is == 0 here is my code in the view.
I can print if active is yes or no.. But in the each do loop i just want to print the active candidates.
So how can i add the condition to my each do loop, thanks.
<% #candidates.each do |candidate| %>
<div id="candidateper">
<div class="avatth" ><div class="avat_min">
<% if candidate.avatar.present? %>
<%= link_to (image_tag candidate.avatar.url(:thumb)), (candidate_path(candidate)) %>
<% else %>
<%= link_to (image_tag ("espanol/playersample.png")), (candidate_path(candidate)) %>
<% end %>
</div></div>
<div class="nameth"><%= candidate.name %></div>
<div class="activeth"><%= candidate.active ? t('generales.yess') : t('generales.noo') %></div>
<div class="generalth">
<% if candidate.user.purchased_at.present? %>
<%= candidate.user.purchase_defeated? ? t('generales.defeated') : t('generales.active') %>
<% else %>
<%= t('generales.noo') %>
<% end %>
</div>
<div class="actionsth"><%= link_to t('generales.show'), candidate_path(candidate) %>
<% if current_user.user_type == 'admin' %>
<%= link_to t('generales.delete'), candidate_path(candidate), method: :delete, data: { confirm: t('generales.delete_candidate_confirm') } %>
<% end %>
</div>
</div>
<% end %>
</div>
<% end %>
I`ve tried this
no luck syntax error on all my ideas :P
If candidate.active is actually a boolean then you could say:
<% #candidates.reject(&:active).each do |candidate| %>
...
<% end %>
If #candidates is actually an ActiveRecord::Relation then you could probably say:
<% #candidates.where(:active => false).each do |candidate| %>
...
<% end %>
to avoid pulling a bunch of stuff out of the database when you don't want it.
If active is actually a number (inside the database and outside the database) then you could say:
<% #candidates.select(&:zero?).each do |candidate| %>
...
<% end %>
or
<% #candidates.where(:active => 0).each do |candidate| %>
...
<% end %>

Regular Expression problem in erb Rails

I need to apply a regular expression in html.erb
<% taxon_name.strains.each do |strain| %>
<% taxon_name.strain_appendices.each_with_index do |strain_appendice, i| %>
<% if ((strain_appendice.data_source =~ /LPSN/) && (strain.relevance =~ /^ty(.*)ain$/))%>
<% if i == 0 %>
<p><%= strain_appendice.appendix %> </p> <% strain.strain_id %> <% strain.relevance %>
<%else%>
<%= strain_appendice.appendix %> - <% strain.strain_id %> <% strain.relevance %>
<%end%>
<%end%>
This code replaces the all content of a strain.strain_id & strain.relevance and doesnot passes match criteria. Instead of matching it replacing strain.strain_id & strain.relevance contents.
Kindly tell me the way to match and pass conditions.
--
With Regards,
Palani Kannan. K,
This code is working for me if I add two <%end%> statements at the end, like this:
<% taxon_name.strains.each do |strain| %>
<% taxon_name.strain_appendices.each_with_index do |strain_appendice, i| %>
<% if ((strain_appendice.data_source =~ /LPSN/) && (strain.relevance =~ /^ty(.*)ain$/))%>
<% if i == 0 %>
<p><%= strain_appendice.appendix %> </p> <%= strain.strain_id %> <%= strain.relevance %>
<%else%>
<%= strain_appendice.appendix %> - <%= strain.strain_id %> <%= strain.relevance %>
<% end %>
<% end %>
<%end%>
<%end%>

Resources