【问题标题】:How to customize edit and update action in rails_admin如何在 rails_admin 中自定义编辑和更新操作
【发布时间】:2012-11-10 06:24:27
【问题描述】:

我正在使用 rails_admin,我认为它很棒。不幸的是,我无法覆盖特定模型上的特定操作。 我只需要覆盖一个模型上的编辑和更新行为。有什么想法吗?

【问题讨论】:

标签: ruby-on-rails customization rails-admin controller-action


【解决方案1】:

我不知道你过去尝试过什么,如果你发布它会很有帮助,但你不能尝试这个

config.model 'Model' do
  edit do
    ....
  end

  update do
    ....
  end
end

【讨论】:

    【解决方案2】:

    好吧,考虑一下您要做什么。我相信你也可以使用 ROR 回调来实现它,这会容易得多。

    所以在你的模型文件中

    after_update :custom_action
    
    #define custom_action in the same model
    def custom_action
      #your code goes here
    end
    

    您可能需要检查此操作是否由管理员执行,仅此而已。

    抱歉,迟到了 4 年。但这可能对其他人有所帮助。

    【讨论】:

      【解决方案3】:

      您可以通过点击审核支持来添加其他更新行为。

      在我的例子中,我需要记录特定模型上的某些字段何时发生更改,以及进行更改的用户。我通过以下方式实现了这一目标:

      RailsAdmin.config do |config|
        config.audit_with { @auditing_adapter = MyAuditor.new(self) }
      end
      
      class MyAuditor
        def update_object(object, _abstract_model, user, changes)
          if object.is_a?(SomeModel)
            changes = changes.slice(:important_field, :other_important_field)
            if changes.present?
              # ... log change ...
            end
          end
        end
      
        # other auditor methods (unused)
        def initialize(_controller) end
        def create_object(_object, _abstract_model, _user) end
        def delete_object(_object, _abstract_model, _user) end
        def latest() end
        def listing_for_object(*_args) end
        def listing_for_model(*_args) end
      end
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-04
        • 1970-01-01
        相关资源
        最近更新 更多