4

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 %>
Thomas Taylor
  • 864
  • 8
  • 25

3 Answers3

2

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

Community
  • 1
  • 1
Thomas Taylor
  • 864
  • 8
  • 25
0

You should use link_to_if.

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

Azmat Rana
  • 532
  • 3
  • 11
0

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

Rajarshi Das
  • 11,778
  • 6
  • 46
  • 74
  • Thanks for the answer @rajarshi-das. I've tried the above solution however it doesn't output anything within the block. The above solution renders like this: `/job/33` so I tried: `<%= link_to_if current_user.type == 'Agent', "", "/job/#{job.id}" do %>` and the href is now correct however nothing inside the block is displaying still! – Thomas Taylor Jan 19 '15 at 07:41
  • you have to use like this `<%= link_to_if current_user.type == 'Agent', "/job/#{job.id}", "/job/#{job.id}" do %>` – Rajarshi Das Jan 19 '15 at 07:46
  • Thanks but it still isn't rendering anything inside my block e.g. `

    Job

    <%= job.suburb %>

    <%= job.street_name %>

    <%= job.post_cide %>

    `
    – Thomas Taylor Jan 19 '15 at 07:49
  • I also don't need it rendering any string it only requires a href – Thomas Taylor Jan 19 '15 at 07:50
  • is your current user type is 'Agent' see/debug what it is giving `<%= current_user.type %>` – Rajarshi Das Jan 19 '15 at 07:57
  • Yep my current user type is agent – Thomas Taylor Jan 19 '15 at 22:33