在Rails 3.2应用程序
的Gemfile:
gem "state_machine", "~> 1.1.2"
gem 'ruby-graphviz', :require => 'graphviz'
我成功安装并使用了state_machine gem,只有当我尝试绘制状态图时
rake state_machine:draw CLASS=Entity
它抛出了这个错误:
** Invoke state_machine:draw (first_time)
** Execute state_machine:draw
** Invoke environment (first_time)
** Execute environment
rake aborted!
uninitialized constant StateMachine::Machine::Constants
/Users/joel/.rvm/gems/ruby-1.9.2-p290@biowatts/gems/state_machine-1.1.2/lib/state_machine/machine.rb:1898:in `draw'
/Users/joel/.rvm/gems/ruby-1.9.2-p290@biowatts/gems/state_machine-1.1.2/lib/state_machine/machine.rb:481:in `block (2 levels) in draw'
/Users/joel/.rvm/gems/ruby-1.9.2-p290@biowatts/gems/state_machine-1.1.2/lib/state_machine/machine.rb:480:in `each_value'
/Users/joel/.rvm/gems/ruby-1.9.2-p290@biowatts/gems/state_machine-1.1.2/lib/state_machine/machine.rb:480:in `block in draw'
/Users/joel/.rvm/gems/ruby-1.9.2-p290@biowatts/gems/state_machine-1.1.2/lib/state_machine/machine.rb:472:in `each'
/Users/joel/.rvm/gems/ruby-1.9.2-p290@biowatts/gems/state_machine-1.1.2/lib/state_machine/machine.rb:472:in `draw'
/Users/joel/.rvm/gems/ruby-1.9.2-p290@biowatts/gems/state_machine-1.1.2/lib/tasks/state_machine.rb:27:in `block (2 levels) in <top (required)>'
/Users/joel/.rvm/gems/ruby-1.9.2-p290@global/gems/rake-10.1.0/lib/rake/task.rb:236:in `call'
/Users/joel/.rvm/gems/ruby-1.9.2-p290@global/gems/rake-10.1.0/lib/rake/task.rb:236:in `block in execute'
/Users/joel/.rvm/gems/ruby-1.9.2-p290@global/gems/rake-10.1.0/lib/rake/task.rb:231:in `each'
/Users/joel/.rvm/gems/ruby-1.9.2-p290@global/gems/rake-10.1.0/lib/rake/task.rb:231:in `execute'
/Users/joel/.rvm/gems/ruby-1.9.2-p290@global/gems/rake-10.1.0/lib/rake/task.rb:175:in `block in invoke_with_call_chain'
/Users/joel/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'
/Users/joel/.rvm/gems/ruby-1.9.2-p290@global/gems/rake-10.1.0/lib/rake/task.rb:168:in `invoke_with_call_chain'
/Users/joel/.rvm/gems/ruby-1.9.2-p290@global/gems/rake-10.1.0/lib/rake/task.rb:161:in `invoke'
/Users/joel/.rvm/gems/ruby-1.9.2-p290@global/gems/rake-10.1.0/lib/rake/application.rb:149:in `invoke_task'
/Users/joel/.rvm/gems/ruby-1.9.2-p290@global/gems/rake-10.1.0/lib/rake/application.rb:106:in `block (2 levels) in top_level'
/Users/joel/.rvm/gems/ruby-1.9.2-p290@global/gems/rake-10.1.0/lib/rake/application.rb:106:in `each'
/Users/joel/.rvm/gems/ruby-1.9.2-p290@global/gems/rake-10.1.0/lib/rake/application.rb:106:in `block in top_level'
/Users/joel/.rvm/gems/ruby-1.9.2-p290@global/gems/rake-10.1.0/lib/rake/application.rb:115:in `run_with_threads'
/Users/joel/.rvm/gems/ruby-1.9.2-p290@global/gems/rake-10.1.0/lib/rake/application.rb:100:in `top_level'
/Users/joel/.rvm/gems/ruby-1.9.2-p290@global/gems/rake-10.1.0/lib/rake/application.rb:78:in `block in run'
/Users/joel/.rvm/gems/ruby-1.9.2-p290@global/gems/rake-10.1.0/lib/rake/application.rb:165:in `standard_exception_handling'
/Users/joel/.rvm/gems/ruby-1.9.2-p290@global/gems/rake-10.1.0/lib/rake/application.rb:75:in `run'
/Users/joel/.rvm/gems/ruby-1.9.2-p290@global/gems/rake-10.1.0/bin/rake:33:in `<top (required)>'
/Users/joel/.rvm/gems/ruby-1.9.2-p290@global/bin/rake:19:in `load'
/Users/joel/.rvm/gems/ruby-1.9.2-p290@global/bin/rake:19:in `<main>'
/Users/joel/.rvm/gems/ruby-1.9.2-p290@biowatts/bin/ruby_noexec_wrapper:14:in `eval'
/Users/joel/.rvm/gems/ruby-1.9.2-p290@biowatts/bin/ruby_noexec_wrapper:14:in `<main>'
Tasks: TOP => state_machine:draw
有关如何解决此问题的任何线索?
答案 0 :(得分:2)
肮脏但有效:
unless defined? StateMachine::Machine::Constants::RGV_VERSION
warn "StateMachine::Machine::Constants overriden in #{__FILE__}"
class StateMachine::Machine::Constants
RGV_VERSION = /^[ ]*ruby-graphviz \(([0-9.]+)\)/.match(`cat #{Rails.root.join 'Gemfile.lock'}`)[1]
end
end
答案 1 :(得分:1)
详细说明tsiruu的答案:如果您使用的是rails,只需将他的代码放入初始化程序中
unless defined? StateMachine::Machine::Constants::RGV_VERSION
warn "StateMachine::Machine::Constants overriden in #{__FILE__}"
class StateMachine::Machine::Constants
RGV_VERSION = /^[ ]*ruby-graphviz \(([0-9.]+)\)/.match(`cat #{Rails.root.join 'Gemfile.lock'}`)[1]
end
end