If else Statement in <%= raw {} %> ERB - ruby-on-rails

I have something like this:
<%= raw query.inline_columns.map {|column|
"<td class=\"#{column.css_classes}\">#{column_content(column, issue)} </td>"
}.join %>
I want to add a if else Statement when the css.classes == "assigned_to" how can i do that ?
I tried this but don't work
<%= raw query.inline_columns.map {|column|
<% if #{column.css_classes} == "assigned_to" %>
"<td class=\"#{column.css_classes}\">#{column_content(column, issue)} </td>"
<% else %>
"<td class=\"#{column.css_classes}\">#{column_content(column, issue)} TEST TEST </td>"
<% end %>
}.join %>
I'm a beginner in ERB language sorry.
Thank's for help

Do you need the final result to be one string? Because if not, it should be simpler to just return the html for each inline_columns, like:
<% query.inline_columns.each do|column| %>
<% if column.css_classes == "assigned_to" %>
<td class="<%= column.css_classes %>"> <%= column_content(column, issue) %> </td>
<% else %>
<td class="<%= column.css_classes %>"> <%= column_content(column, issue) %> TEST TEST </td>
<% end %>
<% end %>

Related

Can't print data "name" ruby on rails

I am trying to print data from a object "root" to a table, everythink work fine but not printing this name i dont know why.
Code :
<% root = NameOfDatabase.find_by_d_id(d.id) %>
<% logger.info root.inspect %>
<% if root.nil? %>
<td class="col-4"> <% "-" %> </td>
<% else %>
<td class="col-4"> <% root.name %> </td>
<% end %>
Picture: View
Log file :
...
#<Name**** id: ****, d_id: ****, name: "Alice", dd_id: ****>
Completed in 99ms (View: 7, DB: 33) | 200 OK [https://*********/dd/search_ajax?option=active&query=s001]
As following in root we have data i want only print root.name in table !!
thanks you
You need to use <%= %> instead of <% %>
<% %> will just evaluate the expression, it won't output the result to ERB template
<%= %> will evaluate the expression and outputs to ERB template.
Try the below:
<% root = NameOfDatabase.find_by_student_id(student.id) %>
<% logger.info root.inspect %>
<% if root.nil? %>
<td class="col-4"> <%= "-" %> </td>
<% else %>
<td class="col-4"> <%= root.name %> </td>
<% end %>
Reference:
What is the difference between <%, <%=, <%# and -%> in ERB in Rails?

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>

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

Rails anyway to add an if statement to a loop

In my rails application I have a set of attachments that display, with a main image being one of them. However, I would like to loop through all of them except the main attachment. My view code right now is:
<% if #asset.attachments.size > 0 %>
<table>
<tr>
<% #asset.attachments.each_with_index do |attachment, i| %>
<% if i%5 == 0 %>
</tr><tr>
<%end%>
<td style="width: 150px;" valign="bottom" align="center">
<%= image_tag(attachment.attachment.url(:thumb)) if attachment.is_image? %>
<%= image_tag("/images/Excel.png") if attachment.is_excel? %>
<%= image_tag("/images/Word.png") if attachment.is_word?%>
<br />
<%= link_to attachment.name.to_s,attachment.attachment.to_s %>
</td>
<%end%>
</tr>
</table>
<%end%>
but, I would like to add something like if !main_image to this line of code:
<% #asset.attachments.each_with_index do |attachment, i| %>
I don't know if that is possible though.
This answer is in continuation to answer on this question
Add another method to your model, which return you the attachments which are not main image.
scope :not_main_image, where(:main_image => false)
On a side note, you might want to move all this logic into a helper method:
<%= image_tag(attachment.attachment.url(:thumb)) if attachment.is_image? %>
<%= image_tag("/images/Excel.png") if attachment.is_excel? %>
<%= image_tag("/images/Word.png") if attachment.is_word?%>
<br />
<%= link_to attachment.name.to_s,attachment.attachment.to_s>
Say you create a helper method like:
def link_to_attachment attachment
html = ""
html += image_tag(attachment.attachment.url(:thumb)) if attachment.is_image?
html += image_tag("/images/Excel.png") if attachment.is_excel?
html += image_tag("/images/Word.png") if attachment.is_word?
html += "<br />"
html += link_to(attachment.name.to_s, attachment.attachment.to_s)
html.html_safe
end
Then in you view you can replace it with:
<td style="width: 150px;" valign="bottom" align="center">
<%= link_to_attachment attachment %>
</td>
<% #asset.attachments.each_with_index do |attachment, i| %>
&lt% next if attachment.main_image %>
It depends on how you define the main attachment
<% #asset.attachments.each_index do |attachment, i| %>
<% unless attachment.main? %>
...
<% end %>
<% end %>
OR
<% #asset.attachments.each_index do |attachment, i| %>
<% if attachment.main? %>
<% next %>
<% else %>
...
<% end %>
<% end %>
OR put this in your controller
#attachments = #asset.attachments.where(:main => false)
after that, iterate over #attachments instead of #asset.attachments

Conditional tag wrapping in Rails / ERB

What would be the most readable and/or concise way to write this in ERB? Writing my own method isn't preferable, as I would like to spread a cleaner solution for this to others in my company.
<% #items.each do |item| %>
<% if item.isolated? %>
<div class="isolated">
<% end %>
<%= item.name.pluralize %> <%# you can't win with indentation %>
<% if item.isolated? %>
</div>
<% end %>
<% end %>
== Update ==
I used a more generic version of Gal's answer that is tag agnostic.
def conditional_wrapper(condition=true, options={}, &block)
options[:tag] ||= :div
if condition == true
concat content_tag(options[:tag], capture(&block), options.delete_if{|k,v| k == :tag})
else
concat capture(&block)
end
end
== Usage
<% #items.each do |item| %>
<% conditional_wrapper(item.isolated?, :class => "isolated") do %>
<%= item.name.pluralize %>
<% end %>
<% end %>
If you really want the DIV to be conditional, you could do something like this:
put this in application_helper.rb
def conditional_div(options={}, &block)
if options.delete(:show_div)
concat content_tag(:div, capture(&block), options)
else
concat capture(&block)
end
end
which then you can use like this in your view:
<% #items.each do |item| %>
<% conditional_div(:show_div => item.isolated?, :class => 'isolated') do %>
<%= item.name.pluralize %>
<% end %>
<% end %>
Try:
<% #items.each do |item| %>
<div class="<%= item.isolated? 'isolated' : '' %>">
<%= item.name.pluralize %>
</div>
<% end %>
I like PreciousBodilyFluids' answer, but it doesn't strictly do exactly what your existing method does. If you really cannot have a wrapping div, this may be prefereable:
<% #items.each do |item| %>
<% if item.isolated? %>
<div class="isolated">
<%= item.name.pluralize %>
</div>
<% else %>
<%= item.name.pluralize %>
<% end %>
<% end %>
A helper method to do all of this would probably look like this:
def pluralized_name_for(item)
if item.isolated?
content_tag(:div, item.name.pluralize, :class => 'isolated')
else
item.name.pluralize
end
end
Then your view code would look like this:
<% #items.each do |item| %>
<%= pluralized_name_for(item) %>
<% end %>

Resources