0

我们将整个aasm块放在字符串中并在payment_request 模型中对其进行评估。这是定义:

class PaymentRequest < :ActiveRecord::Base
  include AASM

  def self.load_wf_spec(wf_spec, wf_def_name)
     eval("aasm(:#{wf_def_name})  :column => 'wf_state' {#{wf_spec}}")
  end
end

错误是:

 Failure/Error: eval("aasm(:#{wf_def_name})  :column => 'wf_state' {#{wf_spec}}")

  SyntaxError:
    (eval):1: syntax error, unexpected ':', expecting end-of-input
    aasm(:test)  :column => 'wf_state' {state :...
                  ^
  # ./app/models/payment_requestx/payment_request.rb:11:in `eval'

这是变量的值:

  wf_def_name = 'test'
  wf_spec = "state :initial_state, :initial => true
       state :ceo_reviewing 
       state :approved
       state :stamped
       state :paid
       state :rejected
      event :submit_test do
         transitions :from => :initial_state, :to => :ceo_reviewing
      end
      event :ceo_approve_test do
         transitions :from => :ceo_reviewing, :to => :approved
      end 
      event :ceo_reject_test do
        transitions :from => :ceo_reviewing, :to => :rejected
      end
      event :ceo_rewind_test do
        transitions :from => :ceo_reviewing, :to => :initial_state
      end
      event :stamp_test do
        transitions :from => :approved, :to => :stamped
      end
      event :pay_test do
        transitions :from => :stamped, :to => :paid
      end"

如果删除 (:test)',则同样的错误指向下一个:

Failure/Error: eval("aasm :column => 'wf_state' {#{wf_spec}}")

  SyntaxError:
    (eval):1: syntax error, unexpected '{', expecting end-of-input
    aasm :column => 'wf_state' {state :initial_state, :initial => true
                                ^
  # ./app/models/payment_requestx/payment_request.rb:11:in `eval'

评估中缺少什么?

4

1 回答 1

0

这是它的工作原理:

eval("aasm(:#{wf_def_name}, :column => 'wf_state') {#{def_string}}")
于 2016-04-15T00:02:58.057 回答