1

validates_with を使用して最初のビジネス ルールを作成しようとしています。作業指示書のステータスが「WAPPR」(承認待ち) の場合、イベント (労働) を入力したくありません。

これは私のイベントモデルにあります:

class Event < ActiveRecord::Base
  validates_presence_of :workorder_id
  validates_with ::WOStatusValidator
  ...
end

class WOStatusValidator < ActiveModel::Validator
  def validate(record)
    if record.workorder.wostatus.statuscode == "WAPPR"
      record.errors[:base] << "Can't enter labor if workorder status is WAPPR"
    end
  end
end

しかし、私はこれを取得します:

undefined method `key?' for nil:NilClass

助けてくれてありがとう!!

アップデート

ラック セッションには次のようなものがあります。

{"session_id"=>"52c6e4be5eeba78e5954a2997d9cac73", "_csrf_token"=>"kf1CYslb/rVmTWjoD4Qn4l1vZCeCwQOgg3VEYLqNTTg=", "warden.user.user.key"=>["ユーザー", [11], "$2a$10$EHEjfNIR5BR5js1 .oJ2."], "flash"=>#, @closed=false, @flashes={:workorder_id=>2, :employee_id=>5, :comment=>true}, @now=nil>}

また、workorder/2 には wostatus/235 があり、その wostatus には statuscode = WAPPR があります

action_name は「new」です

エラーは、visible_action?(gem) actionpack-3.2.12/lib/action_controller/metal/hide_actions.rb のこのコードから発生します。

36行目

35       def visible_action?(action_name)
36         return @visible_actions[action_name] if @visible_actions.key?(action_name)
37         @visible_actions[action_name] = !hidden_actions.include?(action_name)
38       end

更新2

jvnill が提案した:

record.errors.add :base, "Can't enter labor if workorder status is WAPPR"
  • 私はその変更を行い、今私は得る

    「初期化されていない定数 WOStatusValidator」

これは、このコードの 11 行目からのものです。

8 class RoutingError < ActionControllerError #:nodoc: 
9 attr_reader :failures 
10 def initialize(message, failures=[]) 
11 super(message) 
12 @failures = failures 13 end 14 end

UDPATE3

このビジネス ルールを適用するために、適切なタイプのコーディングを使用していますか? より良い方法はありますか?

4

1 に答える 1

1

交換:

validates_with WOStatusValidator

と:

validates_with ::WOStatusValidator

トップレベルの定数を探すように Rails に明示的に指示します。

于 2013-04-05T15:11:41.757 に答える