如何在rails_admin中自定义编辑和更新操作

时间:2012-11-21 22:04:39

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

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

3 个答案:

答案 0 :(得分:1)

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

所以在您的模型文件中

after_update :custom_action

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

您可能必须检查此操作是否由管理员执行,就是这样。

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

答案 1 :(得分:0)

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

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

  update do
    ....
  end
end

答案 2 :(得分:0)

您可以通过访问“审核”支持来添加其他更新行为。

就我而言,我需要记录在特定模型上更改某些字段的时间以及进行更改的用户。我是通过以下方式实现的:

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