Rails动态state_machine

时间:2016-01-23 11:52:54

标签: ruby-on-rails ruby

在我的rails应用程序中,我正在使用gem state_machine,并希望使所有状态动态化。为此,我创建了一个哈希:

STATES = {
  :upload => {:step => 0, :text => 'Upload your file!'},
  :examination => {:step => 1, :text => 'Examine uploaded file!'},
  :download_model => {:step => 2, :text => 'Download created model!'},
  :choice => {:step => 3, :text => 'Choose different settings for your product!'},
  :payment => {:step => 4, :text => 'Pay for your product!'},
  :creation => {:step => 5, :text => 'Your product is currently assembled!'},
  :shipping => {:step => 6, :text => 'Your order was completed!'}
}

现在我想迭代哈希来动态创建状态:

state_machine :state, :initial => STATES[:upload][:step] do
STATES.each_with_index do |state, index|
  state state[0].to_sym do
    def form_fields
      [state[0]]
    end

    def progress
      return {
          :step => index,
          :name => state[0][:text]
      }
    end
  end
end
end

重新启动rails服务器后,出现以下错误:

:upload state defined as Symbol, 0 defined as Fixnum; all states must be consistent

如何使用此哈希来获得动态状态?

0 个答案:

没有答案