Redmine:在插件中扩展控制器动作

时间:2013-08-26 07:26:12

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

在插件开发方面需要一些帮助。 我在用户/编辑表单视图中创建了钩子,添加了ballance_amount到表单并拥有"ballance_amount"=>"1"

如何在用户控制器中扩展默认更新操作?

base.class_eval do我添加了alias_method_chain :update, :ballanceInstanceMethods

def update_with_ballance
    ballance.amount = params[:user][:ballance_amount].to_f #I have ballance association
end

得到这个:

NameError (undefined local variable or method `params' for #<User:0x007f972e9379d0>):
app/controllers/users_controller.rb:144:in `update'

我如何获取参数?

1 个答案:

答案 0 :(得分:0)

您应该能够使用Redmine本身的质量分配代码。如果balance_amount被视为safe_attribute,则Line 135 in the UsersController应该为您的扩展程序提供简单的切入点。为此,请将以下补丁添加到用户模型中:

module RedmineBalancePlugin::UserPatch
  def self.included(base)
    base.class_eval do
      safe_attributes 'balance_amount'

      include InstanceMethods
    end
  end

  module InstanceMethods
    # This method is indirectly called when creating or updating a User
    def balance_amount=(amount)
      balance.amount = amount
    end

    # This could be useful in your view patch, but maybe not
    def balance_amount
      balance.amount
    end
  end
end

User.send(:include, RedmineBalancePlugin::UserPatch)

如果此示例没有帮助,如果您可以提供更多代码片段,那将会很有帮助 - 例如UsersController的完整补丁,以便更容易地重现和分析问题。