Regular Expression problem in erb Rails - ruby-on-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%>

Related

There is a space between the elements 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>

Rails: how to create a list with conditional links

I am working on a RAILS application where I create a view which lists all available resources of a given model species.rb.
The view partial is:
<% i= #s
for species in #species %>
<%= species.name %>, <%= species.author.surname %> <%= species.author.initial_name %>
<% i -= 1
end %>
Some of the resources species have an related article, others have only the name. I would like to loop through the partial and add a link only to the entries which have an associated article.
Something like: add link if species.article is present else just put species.name without link + loop through it for all entries.
How can I do this?
UPDATE:
Thanks to #jvillian and #fool-dev I was able to progress. In my case I wanted to add a link if the resource has a description in a description row of its table.
<% #species.each do |species| %>
<div class="entry">
<p><i><%= link_to_if(species.txtlandscape.present?, "#{species.name}, #{species.author.surname}, #{species.author.initial_name}. 2014", :controller => 'projects', :action => 'show', :id => species) %></i></p>
</div>
<% end %>
Now that a link is added I was wondering if it can be used to load a partial to a target such as in, where ArticleRequest is a JS function I have:
<% # species.each do | species | %>
<div id="species-<%= species.id %>" class="species-entry">
<a onClick="ArticleRequest('/species/show/<%= species.id %>', 'species-<%= species.id %>');">
<p><%= species.name %></p>
</a>
</div>
<% end %>
Until I find a way to do this with link_to_if, I will use something like:
<% for species in #species %>
<% if species.txtlandscape.present? %>
<a onClick="ArticleRequest('/species/show/<%= species.id %>', 'species-<%= species.id %>');">
<p><%= species.name %>, <%= species.author.surname %> <%= species.author.initial_name %></p>
</a>
<% else %>
<p><%= species.name %>, <%= species.author.surname %> <%= species.author.initial_name %></p>
<% end %>
<% end %>
According to the docs, it seems you should be able to do:
<% #species.each do |specie| %>
<%= link_to_if(specie.article, specie.name, specie_article_path(specie.article)) %>
<% end %>
I made the path name up, you'll have to make that match your actual routes.
BTW, this:
for species in #species
Is super non-idiomatic.
You can do this, see the below
<% for species in #species %>
<% if species.article.present? %> #=> I thin it will be articles because table name is articles, anyway, you know better
<%= link_to species.name, link_path(species.article) %>, #=> on the link_path it will be your proper link just replace this
<% else %>
<%= species.name %>,
<% end %>
<%= species.author.surname %> <%= species.author.initial_name %>
<% end %>
You can do this with Rails each method like below
<% #species.each do |species| %>
<% if species.article.present? %> #=> I thin it will be articles because table name is articles, anyway, you know better
<%= link_to species.name, link_path(species.article) %>, #=> on the link_path it will be your proper link just replace this
<% else %>
<%= species.name %>,
<% end %>
<%= species.author.surname %> <%= species.author.initial_name %>
<% end %>
Or you can use link_to_if it is also most easier to understand
<% #species.each do |species| %>
<%= link_to_if(species.article.present?, "#{species.name},", link_path(species.article)) %>
<%= species.author.surname %> <%= species.author.initial_name %>
<% end %>
Hope it will help.

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

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

Checking an array with views in Rails

I have a rails app with some views and I'd like to display something different if the view is the array doesn't contain any data. At the moment my view looks like this:
<% #trips.each do |trip| %>
<p><%= trip.id %> | <%= trip.name %></p>
<% end %>
But is there a way I can show some different text if this doesn't have anything inside #trips ?
If you use partials:
trips/_trip.html
<p><%= trip.id %> | <%= trip.name %></p>
your view
<%= render #trips || 'No trip' %>
You can use an if/else conditional check:
<% if (#trips != nil) then %>
<p><%= trip.id %> | <%= trip.name %></p>
<% else %>
<p>Some message</p>
<% end %>
or
<% if (#trips.blank?) then %>
<p>Some message</p>
<% else %>
<p><%= trip.id %> | <%= trip.name %></p>
<% end %>
sure, just use an if there, like this
<% if #trips.blank? %>
<p>No trips</p>
<% else %>
<% #trips.each do |trip| %>
<p><%= trip.id %> | <%= trip.name %></p>
<% end %>
<% end %>
Use a if statement and check if it is null or empty like this
<% if #trips.nil? || #trips.empty? %>
<p>There are no trips</p>
<% else %>
<% #trips.each do |trip| %>
<p><%= trip.id %> | <%= trip.name %></p>
<% end %>
<% end %>
Edit
If working withing rails you would probably want to go with #trips.blank?, if you decide on another ruby framework or project then you can use the above if/else code to check if a array is null or empty

Resources