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:
<% @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 %>
def link_to_if(*args,&block)
args.insert 1, capture(&block) if block_given?
super *args
end
Reference: https://stackoverflow.com/a/25916594/2811283