Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: Disabling link_to link not working but hidden link_to working

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.

like image 581
codemilan Avatar asked Nov 20 '15 07:11

codemilan


3 Answers

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..

like image 90
przbadu Avatar answered Oct 10 '22 07:10

przbadu


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

like image 28
Long Nguyen Avatar answered Oct 10 '22 06:10

Long Nguyen


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();
  });
like image 20
Sami Birnbaum Avatar answered Oct 10 '22 06:10

Sami Birnbaum