Conditional link_to block in Rails if condition is met - ruby-on-rails

I would like to wrap a conditional link_to around some code that only renders the link if the following condition is met: IF current_user.type == 'Agent'. The content within the condition still needs to be rendered regardless.
My current block looks like this:
<% #jobs_published.in_groups_of(3, false) do |job| %>
<div class="row">
<%= link_to "/job/#{job.id}" do %>
<div class="panel">
<h4>Job</h4>
<p><%= job.suburb %></p>
<p><%= job.street_name %></p>
<p><%= job.post_cide %></p>
</div>
<% end %>
</div>
<% end %>

I solved this by doing the following:
View:
<% #jobs_published.in_groups_of(3, false) do |job| %>
<div class="row">
<%= link_to_if current_user && current_user.type == 'Agent', { controller: "agents", action: "job", :id => job.id } do %>
<div class="panel">
<h4>Job</h4>
<p><%= job.suburb %></p>
<p><%= job.street_name %></p>
<p><%= job.post_cide %></p>
</div>
<% end %>
</div>
<% end %>
ApplicationHelper:
def link_to_if(*args,&block)
args.insert 1, capture(&block) if block_given?
super *args
end
Reference: https://stackoverflow.com/a/25916594/2811283

You can try the below one
<% #jobs_published.in_groups_of(3, false) do |job| %>
<div class="row">
<%= link_to_if(current_user.type == 'Agent', "/job/#{job.id}") do %>
<div class="panel">
<h4>Job</h4>
<p><%= job.suburb %></p>
<p><%= job.street_name %></p>
<p><%= job.post_cide %></p>
</div>
<% end %>
</div>
<% end %>
There are many conditional link_to element
link_to_if , link_to_unless, link_to_unless_current

You should use link_to_if.
Here is the documentation link. http://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to_if

Related

Render only when i set it to render

so my question is how can i enable this piece of code only when i want? I have multiple pages and some pages i need to remove that piece of code.
This is my current partial _navbar.html.erb and div.spotlight is the piece of code i need to disable in some pages
<div class="container">
<nav class="logonav">
<div class="row">
<div class="col50"><span class="logo"></span>
<h2>Musicus</h2>
<p>De ti para o mundo</p>
</div>
<% if user_signed_in? %>
<div class="col50">
<%= link_to 'Logout', destroy_user_session_path, class: 'ui-btn btn-normal', method: :delete %>
</div>
<% else %>
<div class="col50">
<%= link_to new_user_registration_path, class: 'ui-btn btn-accent' do %>
<span class="icon-add-user"></span> Criar Conta
<% end %>
<%= link_to new_user_session_path, class: 'ui-btn btn-normal' do %>
<span class="icon-login"></span> Login
<% end %>
</div>
<% end %>
</div>
</nav>
<div class="spotlight">
<%= yield :other_message %>
<%= render 'search' %>
<%= yield :primary_message %>
</div>
<%= yield :other_nav %>
</div>
You can use a simple if statement:
<% if show_spotlight %>
<div class="spotlight">
<%= yield :other_message %>
<%= render 'search' %>
<%= yield :primary_message %>
</div>
<% end %>

Mailboxer gem, "undefined method `participants' for nil:NilClass"

#conversation doesn't seem to load the actual conversation. I get a nilClass error when loading an individual conversation and I'm not sure why. I feel like I'm missing something silly.
Here is the error (which persists to any use of conversation in the show view):
undefined method `participants' for nil:NilClass
Extracted source (around line #1):
<% conversation.participants.each do |participant| %>
<% unless participant == current_user %>
<%= gravatar_for participant %>
<% end %>
<% end %>
Rails.root: /home/alex/workspace/meetexplore_app
Application Trace | Framework Trace | Full Trace
app/views/conversations/_participants.html.erb:1:in `_app_views_conversations__participants_html_erb___865731980245228929_7035 9899418680'
app/views/conversations/show.html.erb:4:in `_app_views_conversations_show_html_erb__775537665966091139_41963900'
My conversations/show.html.erb file:
<% page_header "Conversation" %>
<p>Chatting with
<%= render 'conversations/participants', conversation: #conversation %>
</p>
<div class="panel panel-default">
<div class="panel-heading"><%= #conversation.subject %></div>
<div class="panel-body">
<div class="messages">
<% #conversation.receipts_for(current_user).each do |receipt| %>
<div class="media">
<% message = receipt.message %>
<div class="media-left">
<%= gravatar_for message.sender, 45, message.sender.name %>
</div>
<div class="media-body">
<h6 class="media-heading"><%= message.sender.name %>
says at <%= message.created_at.strftime("%-d %B %Y, %H:%M:%S") %></h6>
<%= message.body %>
</div>
</div>
<% end %>
</div>
</div>
</div>`
My _participants.html.erb partial:
<% conversation.participants.each do |participant| %>
<% unless participant == current_user %>
<%= gravatar_for participant %>
<% end %>
<% end %>
Yet this index works:
<% page_header "Your Conversations" %>
<p><%= link_to 'Start conversation', new_message_path, class: 'btn btn-lg btn-primary' %></p>
<ul class="list-group">
<%= render partial: 'conversations/conversation', collection: #conversations %>
</ul>
<%= will_paginate %>
_conversation.html.erb partial:
<li class="list-group-item clearfix">
<%= link_to conversation.subject, conversation_path(conversation) %>
<p><%= render 'conversations/participants', conversation: conversation %></p>
<p><%= conversation.last_message.body %>
<small>(<span class="text-muted"><%= conversation.last_message.created_at.strftime("%-d %B %Y, %H:%M:%S") %></span>)</small></p>
</li>
So I was missing the before_action and function get_conversation to actually find the conversation with the proper id... woops!

Break out of conditional block in 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 %>

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 Odd Even If Statement

I have a loop for my Activity model
<% #activities.each do |activity| %>
<div>
<div class="cell">
<%= image_tag(activity.image) %>
</div>
<div class="cell">
<h4><%= activity.title %></h4>
<p><%= activity.description %></p>
<span><%= activity.distance %></span>
</div>
</div>
<% end %>
I want to create a zig-zag effect, so I need to re-arrange the HTML for every even activity, so the markup will look like this:
<div>
<div class="cell">
<h4><%= activity.title %></h4>
<p><%= activity.description %></p>
<span><%= activity.distance %></span>
</div>
<div class="cell">
<%= image_tag(activity.image) %>
</div>
</div>
Is there a way to do this with an if statement? Something like this:
<% if activity.odd? %>
<% #activities.each do |activity| %>
<div>
<div class="cell">
<%= image_tag(activity.image) %>
</div>
<div class="cell">
<h4><%= activity.title %></h4>
<p><%= activity.description %></p>
<span><%= activity.distance %></span>
</div>
</div>
<% end %>
<% else %>
<% #activities.each do |activity| %>
<div>
<div class="cell">
<h4><%= activity.title %></h4>
<p><%= activity.description %></p>
<span><%= activity.distance %></span>
</div>
<div class="cell">
<%= image_tag(activity.image) %>
</div>
</div>
<% end %>
There are a few ways to do this.
The first and best way is to do this with CSS's nth-child selector, assuming it's possible to achieve your desired effect purely through CSS. Use nth-child(odd) or nth-child(even), as described in Alternate table row color using CSS?
If you're iterating over a collection using .each, you can add a .with_index and check whether the index is odd or even:
<% #activities.each.with_index do |activity, index| %>
<% if index.odd? %>
...
<% else %>
...
<% end %>
<% end %>
Note that by default the index is zero-based, so your first element will be considered "even". You can pass a starting index of 1 to with_index if you want the alternating to start odd instead:
<% #activities.each.with_index(1) do |activity, index| %>
An alternative to using with_index is to use Rails' cycle helper, which "magically" returns the next argument in rotation each time its invoked:
<% #activities.each do |activity| %>
<% if cycle('odd', 'even') == 'odd' %>
...
<% end %>
<% end %>
Also note that this is a fantastic opportunity for refactoring. You should distill the two reused cell divs into their own partials and render them in the order you want:
<% if index.odd? %>
<%= render h4_part %>
<%= render img_part %>
<% else %>
<% render img_part %>
<% render h4_part %>
<% end %>

Resources