sam roberts
sam roberts

Reputation: 217

Rails link_to unless something then link elsewhere

So i have this at the moment

<%= link_to 'Compare', event_path(event.id), class: "btn btn-info" %>

simple link to, Nothing fancy.

However, What im wanting to do, Is when users click the button to compare, If the event.id has a value inside column_a it will redirect to the /eventinfo/(data value inside column_a).html.erb

How do i go about this?

This is currently ona results page populated from searchkick if that helps at all?

Sam

Upvotes: 0

Views: 222

Answers (1)

Kristj&#225;n
Kristj&#225;n

Reputation: 18843

link_to only builds a link; you'll need to implement this logic yourself. The event.id is a number, so it can't have a value in column_a - I assume you meant column_a exists on event.

<%- if event.column_a.present? -%>
  <%= link_to 'Compare', eventinfo_path(event.column_a) %>
<%- else -%>
  <%= link_to 'Compare', event_path(event.id) %>
<%- end -%>

For a clean template or reuse, you could put this logic into a helper method that either generates the whole link or selects the desired URL.

Upvotes: 1

Related Questions