首页 > 解决方案 > 在 Rails 5.2.3 中替换 'alias_method_chain :save_attachments, :pasted_images'

问题描述

我正在运行一个 Redmine 3.4.4 安装,它使用 Ruby 2.2.5-p319 和 Rails 4.2.8。我们想将它升级到需要 Rails 5 的 Redmine 最新版本(当前为 4.0.4)。

我正在使用 Ruby 2.6.3-p62 和 Rails 5.2.3 使用 4.0.4 运行新服务器。总体来说还可以,但是我们安装了许多想要迁移的插件。由于 Rails 5 中的弃用,其中许多都遇到了问题。即使我以前没有编写过 Ruby on Rails,我也设法通过 9 个插件中的 8 个,但我被困在最后一个插件上,只是想不通。

该插件是我的页面自定义插件,当我尝试迁移数据库和插件时,我收到此错误:

[centos@redmine]$ bundle exec rake db:migrate RAILS_ENV=production
rake aborted!
NoMethodError: undefined method `alias_method_chain' for ActivitiesController:Class
Did you mean?  alias_method
/usr/local/src/redmine-4.0.4/plugins/redmine_my_page/lib/my_page_patches/activities_controller_patch.rb:11:in `block in included'
/usr/local/src/redmine-4.0.4/plugins/redmine_my_page/lib/my_page_patches/activities_controller_patch.rb:7:in `class_eval'
/usr/local/src/redmine-4.0.4/plugins/redmine_my_page/lib/my_page_patches/activities_controller_patch.rb:7:in `included'
/usr/local/src/redmine-4.0.4/plugins/redmine_my_page/init.rb:30:in `include'
/usr/local/src/redmine-4.0.4/plugins/redmine_my_page/init.rb:30:in `block (2 levels) in <top (required)>'

因此,很明显,已弃用'alias_method_chain'是这里的问题。经过一番挖掘,我在网上找到了很多参考资料,例如这个很好而且很清楚的参考资料,但我就是无法编写有效的代码 - 我不断收到语法错误并且无法弄清楚我做错了什么。

这是来自的原始片段activities_controller_patch.rb:

  module ActivitiesControllerPatch

    def self.included(base) # :nodoc:
      base.send(:include, InstanceMethods)

      base.class_eval do
        unloadable
        helper :issues
        helper :queries
        alias_method_chain :index, :esi
      end
    end

如果可以的话,我们想继续使用这个插件,即使它没有正式支持 Redmine 4。我希望有更好的 Ruby 知识的人能够提供帮助。

标签: ruby-on-rails-5redmine-plugins

解决方案


代替

alias_method_chain :index, :esi

你只需使用

alias_method :index_without_esi, :index
alias_method :index, :index_with_esi

这是某种语法糖。


推荐阅读