【问题标题】:Can I customize the delete warning in ActiveAdmin我可以在 ActiveAdmin 中自定义删除警告吗
【发布时间】:2014-08-15 12:55:09
【问题描述】:

在 Rails 应用程序中,Destroy 操作伴随着警告是很常见的——“您确定要删除它吗?”典型代码如下所示:

link_to("Destroy", "http://www.example.com", :method => :delete, :confirm => "Are you sure?")

ActiveAdmin 中是否有一种方法可以仅为一个模型(不是全局)自定义确认字符串?我看到字符串是从active_admin.delete_confirmation 翻译键加载的。字符串可以是特定于模型的吗?

【问题讨论】:

    标签: ruby-on-rails-3 activeadmin


    【解决方案1】:

    由于 ActiveAdmin 2.7 模型特定的翻译可以通过将它们放置到 en.yml 中的 active_admin.resources 组来自定义。

    active_admin:
        resources:
            user:
                delete_confirmation: Are you sure?
    

    【讨论】:

      【解决方案2】:

      我认为您不能通过配置更改字符串。 但是您可以更改默认操作:

      索引表:https://github.com/activeadmin/active_admin/blob/master/docs/3-index-pages/index-as-table.md

      index do
        column :title
        actions defaults: false do |post|
          link_to("Destroy", "http://www.example.com", :method => :delete, :confirm => "Are you sure?")
        end
      end
      

      显示页面:https://github.com/activeadmin/active_admin/blob/master/docs/8-custom-actions.md#action-items

      action_item only: :show do
        link_to("Destroy", "http://www.example.com", :method => :delete, :confirm => "Are you sure?")
      end
      

      【讨论】:

      • 原来我们在 0.4.3 (eek!) 所以我不得不使用 different syntax 但你是对的,这就是这样做的方法。
      【解决方案3】:

      您还可以通过 Javascript 克服此 ActiveAdmin 限制。

      这里是app/assets/javascripts/active_admin.js的例子:

      //= require active_admin/base
      
      $( document ).ready(function() {
      
          $('body.admin_companies a[data-method=delete]').
              data('confirm', 'New confirmation text per model Company.\n Ok?');
      
          $('body.admin_users a[data-method=delete]').
              data('confirm', 'New confirmation text per model User.\n Ok?');
      });
      

      其中body.admin_companies 是将此确认消息限制为模型公司的视图。 body.admin_users 类似。

      它适用于#index 和#show。

      唯一的缺点是它不能很好地与 i18n 配合使用(如果您需要多语言,请避免使用此解决方案)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-07-06
        • 2021-12-12
        • 1970-01-01
        • 2018-05-08
        • 1970-01-01
        • 1970-01-01
        • 2014-04-30
        相关资源
        最近更新 更多