开拓者操作未定义的方法`call'为零:NilClass

时间:2017-12-29 08:09:22

标签: ruby trailblazer

我有一个简单的开拓者操作如下:

class User::Delete < Trailblazer::Operation
  extend Contract::DSL

  contract 'params' do
    property :token

    validates :token, presence: true
  end

  step    Contract::Validate(name: 'params'), before: 'operation.new'
  success :kill_zombie

  def kill_zombie(options, params:, **)
    options['killed'] = true
  end

end

当我在rails控制台下运行操作时,输出结果为:

irb(main):003:0> User::Delete.()
NoMethodError: undefined method `call' for nil:NilClass
    from (irb):3

如果我删除合同行:

class User::Delete < Trailblazer::Operation
  extend Contract::DSL

  contract 'params' do
    property :token

    validates :token, presence: true
  end

  success :kill_zombie

  def kill_zombie(options, params:, **)
    options['killed'] = true
  end

end

按预期工作:

irb(main):005:0> User::Delete.()
=> <Result:true <Skill {"killed"=>true} {"params"=>{}} {"pipetree"=>[>operation.new,>kill_zombie], "contract.params.class"=>#<Class:0x0000000003e54e50>}> >
irb(main):006:0>

有什么想法吗?

2 个答案:

答案 0 :(得分:2)

使用改革合同时,Contract::Build之前需要Contract::Validate。使用Dry架构,不需要构建步骤。

答案 1 :(得分:1)

我会深入研究文档http://trailblazer.to/gems/operation/2.0/contract.html

该消息表示you are trying to call a method on a nil object

对象为User::Delete,方法为.()

User::Delete.()

User::Delete可能未定义,因为它未通过验证?

step    Contract::Validate(name: 'params'), before: 'operation.new'

只是张贴给你一些输入,我没有解决方案。在调用方法之前,我会检查User::Delete

User::Delete将为nil,您需要更改验证或确保对象已正确实例化。