In Rails 4, how do I disable a button in a view and also ensure it cannot be clicked?

538 Views Asked by At

I’m using Rails 4.2. I want to disable a button and render it unclickable in my ERB, subject to certain conditions. I tried this

    <%= link_to 'Submit Form', order_products_path, method: :post, data: {confirm: "Are you sure?"},
                class: 'btn btn-class1', disabled: submit_disable %>

And although the button on the UI appears as disabled, it is still clickable (when I click on it, the "Are you sure?" confirmation appears). So then I tried this

    <%= link_to_if !submit_disable, 'Submit Form', order_products_path, method: :post, data: {confirm: "Are you sure?"},
                class: 'btn btn-class1', disabled: submit_disable %>

However now the button appears only as text. What’s the proper way to disable a button and eliminate the ability for a user to click on it?

1

There are 1 best solutions below

0
Lam Phan On

link_to_if will return only name if the condition is false, however you could pass a block as falsy case, so, i think you could do like so

<%= 
 link_to_if !submit_disable, 'Submit Form', 
   order_products_path, method: :post, data: {confirm: "Are you sure?"}, 
   class: 'btn btn-class1' do
     # disabled view
     label_tag 'Submit Form', nil, class: 'btn btn-class1'
 end
%>