【问题标题】:Rails 3. how to use disable_with in button tag?Rails 3. 如何在按钮标签中使用disable_with?
【发布时间】:2012-01-31 17:55:50
【问题描述】:

我最近开始测试 web_app_theme 插件。在创建按钮中我有这样的东西......

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

如何在此按钮上添加:disable_with =&gt; "Processing" 以防止重复记录?

我试过t("web-app-theme.save", :default =&gt; "Save"), :disable_with =&gt; "Processing",但当然没用。

【问题讨论】:

    标签: html ruby-on-rails ruby


    【解决方案1】:

    您需要使用表单标签辅助方法 - 应该这样做:

    <%= 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 %>
    

    这是button_to 方法的API 文档的链接:http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-button_to

    2013 年 12 月 14 日更新 Rails 3.2 和 4 语法。

    【讨论】:

    • 谢谢!只有一个编辑:button_tag 后面不应该有逗号。我尝试编辑,但它告诉我我必须编辑至少 6 个字符
    • 啊 - 是的,感谢您了解这一点。我只是在疯狂地打字!
    • 这会被弃用吗?
    • 它没有被弃用,但旧的实现是 - 我已经更新了答案以包含新的语法。这是一个简单的更新,只需将 :disable_with:confirm 之类的操作放在 :data 哈希中即可。
    【解决方案2】:

    button_tag 完全是可选的。您可以轻松地编辑 html 中的标签,如下所示:

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

    :disable_with 所做的只是为元素添加一个 data-disable-with 属性。然后 jquery-rails gem 的 javascript (jquery_ujs.js) 会为您完成其余的禁用工作。

    当然,这是假设您使用的是 rails 3.0 或更高版本。

    【讨论】:

    • 我可以指定 ajax 处理完成后显示的值,而不是重置为 ajax 之前的值吗?
    • @Dean 并非没有您自己的 javascript,它可以根据需要设置文本。当您说“ajax 处理”时,这是否意味着您正在使用data-remote=true?你应该可以做类似$('button[type=submit]').on('ajax:success', function(e){$(this).val("New Text")})
    猜你喜欢
    • 1970-01-01
    • 2015-12-01
    • 2011-06-20
    • 2016-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-11
    • 2014-07-21
    相关资源
    最近更新 更多