Break out of conditional block in rails - ruby-on-rails

I have to break out of the if condition if the index is greater than 5 after appending the icon in my row. Right now it keeps on adding the icon.
<div class="row sub-navigation">
<% #projects.each_with_index do |project, index| %>
<div class="col-sm-2 col-xs-1">
<% if index > 5 %>
<%= link_to "", path, remote: true, id: "project_div",
class: "glyphicon glyphicon-chevron-down" %>
<% else %>
<%= link_to project.name, project_url(project.project_id), class: ('active' if current_page?(project_path(project.project_id)) ) %>
<% end %>
</div>
<% end %>
</div>

You can use break
<% #projects.each_with_index do |project, index| %>
<% break if index < 5 %>
<% end %>
--
Or you could also use .take as per this answer:
<% #projects.take(5).each do |project| %>
...
<% end %>
This will allow you limit the value of the loop to 5 objects only, preventing the need for further logic
break is a common programming function, designed to get out of a loop
What Dax and I were suggesting was to add it along-side your if statement:
<% if index < 5 %>
<% break %>
<% else %>
... do something
<% end %>
If you just want to add an icon for the first 5 links, you'll want to do this:
<% your_class = index > 5 ? nil : "icon_class" %>
<%= link_to "path", path_helper, class: your_class %>
--
Update
In response to your pastie, here's what you need to do:
<div class="row sub-navigation">
<% #projects.each_with_index do |project, index| %>
<div class="col-sm-2 col-xs-1">
<% your_class = index > 5? "icon_class" : nil %>
<% link_to "", path, remote: true, id: "project_div", class: your_class %>
</div>
</div>

You can try <% break if index > 5 %>

Related

Adding each_slice (3) gives Error in Rails App

I'm working on this do loop in my app, it works fine but I want to take it further (see below) and I'm not sure how to do that.
<div class="row">
<% #products.each do |category, products| %>
<% products.each_with_index do |product, index| %>
<% if index == 0 %>
<div class="col-lg-4 col-sm-6 col-xs-12 center-block " >
<%= image_tag product.image.url(:medium), class: "img-responsive" %>
<div class="caption">
<p><%= product.category.name %></p>
</div>
<% end %>
</div>
<% end %>
<% end %>
</div>
At the moment the loop is looping through 12 categories and displaying one image from each category. I don't want to have all the images appearing in the same row.
I tried to add each_slice(3) to the loop but it gives me this error:
undefined method 'image' for 5:Fixnum
This is how the code looks after the each_slice(3) insertion
<% #products.each_slice(3) do |category, products| %>
<div class="row">
<% products.each_with_index do |product, index| %>
<% if index == 0 %>
<div class="col-lg-4 col-sm-6 col-xs-12 center-block " >
<%= image_tag product.image.url(:medium), class: "img-responsive" %>
<div class="caption">
<p><%= product.category.name %></p>
</div>
<% end %>
</div>
<% end %>
<% end %>
</div>
I'm not sure what to do here or if each_slice (3) is the right way to do this.
array = [..]
array.each_slice(x) will give you array.size / x arrays different.
Example :
> [1,2,3,4,5,6].each_slice(2) do |g| p g end
[1, 2]
[3, 4]
[5, 6]
So you have to add a loop to your code :
<% #products.each_slice(3) do |products_group| %>
<% products_group.each do |category, products| %>
...
<% end %>
<%end%>

Rails: Cloudinary gem - default image not loading

I'm working on a pinterest-like app, using Cloudinary gem with Attachinary. I have added a line into my index.html.erb file which was supposed to display a default image if a created Pin didn't have one or the image didn't load properly.
<% if pin.image.present? %>
<%= link_to pin do %>
<%= cl_image_tag(pin.image.path) %>
<% end %>
<% if pin.image.present? == false %>
<%= cl_image_tag('no-image-found_jqqruy') %>
<% end %>
Unfortunately it doesn't work - the images for the pins having them display properly, but for ones who don't only a thick line is visible (jQuery Masonry grid). I'm wondering what is the proper way of setting a default image with cloudinary. Where did I go wrong? Here is my full index file:
<div id="pins" class="transitions-enabled">
<% #pins.each do |pin| %>
<div class="container">
<div class="box panel panel-default">
<% if pin.image.present? %>
<%= link_to pin do %>
<%= cl_image_tag(pin.image.path) %>
<% end %>
<% if pin.image.present? == false %>
<%= cl_image_tag('no-image-found_jqqruy') %>
<% end %>
<div class="panel-body">
<%= pin.description %> <br>
<strong><%= pin.user.email if pin.user %></strong>
<% if pin.user == current_user && user_signed_in? %>
<div class="actions">
<%= link_to edit_pin_path(pin) do %>
<span class="glyphicon glyphicon-edit black"></span>
Edit
<% end %>
<%= link_to pin, method: :delete, data: { confirm: 'Are you sure?' } do %>
<span class="glyphicon glyphicon-trash black"></span>
Delete
</div>
<% end %>
<% end %>
</div>
<% end %>
</div>
<% end %>
</div>
I will only add that everything worked fine until I've introduced the glyphicons.
I will be greatful for your help!
If there was no copy&paste error, there is an extra end missing to terminate the link_to pin do statement. So the first if statement is still active, and then an opposite if statement is queried, which of course will never be true.
I suggest to use if-else-end.
<% if pin.image.present? %>
<%= link_to pin do %>
<%= cl_image_tag(pin.image.path) %>
<% end %>
<% else %>
<%= cl_image_tag('no-image-found_jqqruy') %>
<% end %>

Appending a icon based on the project count

I have a row which displays projects, now i need to add an icon to the end if the number of items exceed 6. How can write a condition here to check if the project count is greater than 6 then instead of adding the projects i append a icon.
<div class="row">
<% #projects.each do |project| %>
<div class="col-sm-2 col-xs-1">
<%= link_to project.name, project_url(project.project_id),
class: ('active' if current_page?(project_path(project.project_id)) ) %>
</div>
<% end %>
</div>
You could try using each_with_index and then have a conditional in the loop to detect when you're on the 6th loop (first loop begins with zero for the index value).
Something like this:
<div class="row">
<% #projects.each_with_index do |project, index| %>
<% if index > 5 >
<!-- show icon -->
<% else %>
<div class="col-sm-2 col-xs-1">
<%= link_to project.name, project_url(project.project_id),
class: ('active' if current_page?(project_path(project.project_id)) ) %>
</div>
<% end %>
<% end %>
</div>
I think this is what you want. If not then feel free to ask for change.
<% if #projects.size > 6 %>
###put your icon here
<% else %>
<div class="row">
<% #projects.each do |project| %>
<div class="col-sm-2 col-xs-1">
<%= link_to project.name, project_url(project.project_id),
class: ('active' if current_page?(project_path(project.project_id)) ) %>
</div>
<% end %>
</div>
<% end %>
You can use 'each_with_index' instead of each , like this
<%#images.each_with_index do |page, index| %>
<% if index > 5 %>
//here put your code to add icon
<%else%>
<%= link_to project.name, project_url(project.project_id),
class: ('active' if current_page?(project_path(project.project_id))
<%end%>
<%end%>
I think it will help you

erb block not looping

I'm trying to generate results for a webstore using this erb block and I want 4 images per row, but right now it is only generating one image per row. Any advice would be highly appreciated
<% n = 4 %>
<% #products.each do |product| %>
<div class="row-fluid">
<% if (n%4 == 0) %>
<% end %>
<div class="span3">
<%= link_to image_tag(product.images.order(:placement).first.image.url(:medium)), product if product.images.present? %>
<p class="text-center"><%= link_to product.name, product %></p>
</div>
<% if (n%4 == 3) %>
<% end %>
<% n += 1 %>
</div>
<% end %>
Ruby will break it into groups of four for you using Enumerable#each_slice:
<% #products.each_slice(4) do |row| %>
<div class="row-fluid">
<% row.each do |product| %>
<div class="span3">
<%= link_to image_tag(product.images.order(:placement).first.image.url(:medium)), product if product.images.present? %>
<p class="text-center"><%= link_to product.name, product %></p>
</div>
<% end %>
</div>
<% 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 %>

Resources