0

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

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

base.class_eval do我添加了alias_method_chain :update, :ballance In InstanceMethods

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'

如何获取参数?

4

1 回答 1

0

您应该能够使用 Redmine 本身中的批量分配代码。如果balance_amount被认为是safe_attribute. 为此,请在 User 模型中添加如下补丁:

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 的完整补丁),以便更轻松地重现和分析问题,那将会很有帮助。

于 2013-08-26T19:24:32.783 回答