Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3. how to use disable_with in button tag?

I recently started testing web_app_theme plugin. In Create button I have something like this...

<button class="button" type="submit">
  <%= image_tag("web-app-theme/icons/tick.png", :alt => "#{t("web-app-theme.save", :default => "Save")}") %> <%= t("web-app-theme.save", :default => "Save") %>
</button>

How can I add :disable_with => "Processing" to this button to prevent duplicate records?

I tried t("web-app-theme.save", :default => "Save"), :disable_with => "Processing", but of course, that didn't work.

like image 839
leonel Avatar asked Dec 06 '22 16:12

leonel


2 Answers

You need to use form tag helper methods - something like this should do it:

<%= button_tag :class => "button", :type => "submit", :data => { :disable_with => "Processing" } do %>
  <%= image_tag("web-app-theme/icons/tick.png", :alt => "#{t("web-app-theme.save", :default => "Save")}") %> 
  <%= t("web-app-theme.save", :default => "Save") %>
<% end %>

Here's a link to the API docs for the button_to method: http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-button_to

Updated Dec 14, 2013 for Rails 3.2 & 4 syntax.

like image 70
Matthew Lehner Avatar answered Dec 17 '22 09:12

Matthew Lehner


button_tag is entirely optional. You could just as easily edit the tag in html as follows:

<button class="button" type="submit" data-disable-with="Processing">
  <%= image_tag("web-app-theme/icons/tick.png", :alt => "#{t("web-app-theme.save", :default => "Save")}") %> <%= t("web-app-theme.save", :default => "Save") %>
</button>

All that :disable_with does is add a data-disable-with attribute to the element. Then the jquery-rails gem's javascript (jquery_ujs.js) does the rest of the disabling work for you.

This is assuming, of course, that you're on rails 3.0 or higher.

like image 44
nzifnab Avatar answered Dec 17 '22 08:12

nzifnab