1

I have a link that has a class: 'btn btn-info' style on it. The problem is when I disable the link the bootstrap style doesn't appear I only get a plain text.

<%= link_to_if organisme.active?, 'Show', organismereferent_path(organisme), class: 'btn btn-info', :disabled => !organisme.active? %>

What I tried to do is change the link_to_if for a button_to

<%= button_to 'Show', organismereferent_path(organisme), class: 'btn btn-info', :disabled => !organisme.active? %>

This works but now the button does not send the user to the right place it stays on the same page. What is the correct way of handling this situation?

I.B
  • 2,925
  • 1
  • 9
  • 22
  • Related to https://stackoverflow.com/questions/29379979/disable-a-link-in-bootstrap – Fangxing May 17 '17 at 00:52
  • 1
    Add the `disabled` class if your link is disabled – LukeS May 17 '17 at 00:54
  • @LukeS I'm not too sure I understand what you mean.I'm pretty new to Ruby on Rails isn't this the regular syntax of how to disable something? – I.B May 17 '17 at 01:12
  • it is, but you also need to add `disabled` to your list of classes so that Bootstrap recognizes that it's disabled. The `:disabled => !organisme.active?` will set the disabled html attribute to true but bootstrap doesn't support that. You need to specify it through the `class` attribute. Hope that's clearer :) – LukeS May 17 '17 at 02:18

1 Answers1

4
<% if organisme.active? %>
    <%= link_to 'Show', organismereferent_path(organisme), class: 'btn btn-info'%>
<% else %>
    <%= link_to '#', organismereferent_path(organisme), class: 'btn btn-info disabled', :disabled => true %>
<% end %>

or if the class is enough

<%= link_to 'Show', organismereferent_path(organisme), class: "btn btn-info#{organisme.active? ? ' disabled' : nil }"%>
Alexis
  • 4,836
  • 2
  • 21
  • 27