13

The link_to method is as which is not disabled:-

<%= link_to edit_cabinet_path(object), remote: true, disabled: true do %>
      <span class="glyphicon glyphicon-pencil"></span>
<% end %>  

but if i do like below which hides the link

<%= link_to edit_cabinet_path(object), remote: true, style: "display:none;" do %>
      <span class="glyphicon glyphicon-pencil"></span>
<% end %>  

Now the question is how to disable this type of link with block, and whats the reason that second code is working and first is not.

Promise Preston
  • 24,334
  • 12
  • 145
  • 143
codemilan
  • 1,072
  • 3
  • 12
  • 32
  • Your first link is not working because we can't use `disabled: true` in html links. Your second code will hide your link, because you have set `display: none` css property, so it will not disable the link but hide it. – przbadu Oct 09 '18 at 06:35

4 Answers4

22

Probably, you are looking for link_to_if. link_to_if makes your link clickable only if your condition pass.

Your code should be something like:

<%= link_to_if false, edit_cabinet_path(object), remote: true do %>
      <span class="glyphicon glyphicon-pencil"></span>
<% end %> 

To make it dynamic you can call condition that satisfy whether to active or inactive that link, something like:

<%= link_to_if cabinate.active?, 
               "<span class='glyphicon glyphicon-pencil'></span>".html_safe, 
               edit_cabinet_path(object), remote: true %>

Hope this answer your question..

przbadu
  • 5,769
  • 5
  • 42
  • 67
3

Actually there is no disabled attribute available for link_to, only for button_to tag.

For more information please refer here: http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to

In this case, you might want to use link_to_if, please have a look into this: http://apidock.com/rails/v4.2.1/ActionView/Helpers/UrlHelper/link_to_if

Long Nguyen
  • 11,075
  • 2
  • 18
  • 25
  • if so then why this code works <%= link_to "Complete install", process_work_detail_cabinets_path(work_order: work_order_id, work_detail: work_detail_id), method: :post, class: "btn btn-sm btn-primary progasket pull-right", id: "change_work_detail_install", disabled: true %> – codemilan Nov 20 '15 at 07:39
  • what do you mean by "this code works"? is it hidden from the page or you can see but when click, it does nothing? – Long Nguyen Nov 20 '15 at 07:43
  • you mentioned that disabled attr. is not available for link_to tag only for button_to tag, but link_to method mentioned above in my comment has the disabled attr. working. The link is not hidden only is disabled. – codemilan Nov 20 '15 at 07:46
  • 1
    I don't know how it can work? But I think `link_to_if` is what you need :) Please have a look here: http://apidock.com/rails/v4.2.1/ActionView/Helpers/UrlHelper/link_to_if – Long Nguyen Nov 20 '15 at 07:54
2

I wrote some simple JS to allow you to add disabled: true to link_to method

  //this allows us to use html disabled attribute in rails
  //to prevent clicking on a disabled link from doing anything
  $('a[disabled]').click(function(e){
    e.stopImmediatePropagation()
    e.preventDefault();
  });
Sami Birnbaum
  • 773
  • 8
  • 20
0

link_to_if doesn't work as expected. It only renders the given block when your condition is false as a fallback.
https://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to_if#1589-Passing-a-block-does-not-behave-as-expected

In my case I made a Proc for a given block

<%link_block = Proc.new{%>
<span class="glyphicon glyphicon-pencil"></span>
<%}%>
<%if condition?%>
<%= link_to edit_cabinet_path(object), remote: true, disabled: true, &link_block%>
<%else%>
<%link_block.call%>
<%end%>
andylee
  • 255
  • 2
  • 6