本文介绍了状态机Gem + Rails 4:#< Class:0x007faffc93fd60>的未定义方法"state_machine";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经安装了宝石

gem 'state_machine', :require => 'state_machine/core'

我捆绑了.

我已经将列状态类型字符串迁移到相关的表中.

I've migrated a column state type string to the relevant Table.

我的模特看起来像这样

  belongs_to :user
  belongs_to :patient

  validates :patient_id, uniqueness: {scope: [:email]}

  state_machine :initial => :unseen do
    event :look do
      transition :unseen => :seen
    end
  end

我跑步时

rails c
Table.all

我在引用state_machine的模型页面时遇到以下错误

I got the below error in reference to the model page where I referenced state_machine

undefined method `state_machine' for Table

我注释掉了模型页面的相关部分,删除了该表中所有先前创建的记录,取消注释了模型的state_machine部分,然后从rails c中成功创建了一条记录,但是state的值是零.

I commented out the relevant portion of the model page, deleted all the previously created records in that table, uncommented out the state_machine portion of the model, and then from within rails c I could successfully create a record but the value for state was nil.

当我尝试加载首页时,同样令人不安的是,我使用的是Angular,它进行API调用以检索记录并返回500错误.控制台日志显示

Equally disturbing when I try to load my front page I'm using Angular and it makes an API call to retrieve records which returns a 500 error. The console log shows

NoMethodError (undefined method `state_machine' for #<Class...

此人( state_machine gem与rails 4集成)相同的问题,需要重新启动服务器.我已经多次重启服务器,以及关闭终端然后再启动服务器.

This person (state_machine gem integration with rails 4) had the same issue and needed to restart their server. I have restarted the servers several times, as well as closing terminal and then starting the servers.

我的Gemfile.lock具有此文件-state_machine(1.2.0),并且捆绑包未显示任何错误.

My Gemfile.lock has this file - state_machine (1.2.0), and the bundle showed no errors.

尽管删除了该表中的所有先前记录(需要注释掉状态机代码)之后能够成功调用Table.all,然后在取消注释所述代码后仍然能够创建和查看记录,如果可以的话离开控制台并返回到控制台并重复完全相同的请求(Table.all),这将导致未定义方法'state_machine'的相同错误

Despite having been able to call Table.all successfully after deleting all previous records in that table (requiring commenting out the state machine code) and then, after uncommenting said code, being able to create and view a record, if I then leave the console and return to the console and repeat the exact same request (Table.all) it leads to the same error of undefined method 'state_machine'

推荐答案

我认为您有两个选择.

I think you have two options.

要么删除:require => 'state_machine/core'部分.因此,新行将如下所示:

Either you should drop the :require => 'state_machine/core' part. So the new line would look just like this:

gem 'state_machine'

选项2

或将此行添加到您的班级:

Option 2

Or add this line to your class:

extend StateMachine::MacroMethods

例如:

require 'state_machine/core'

class Vehicle
  extend StateMachine::MacroMethods

  state_machine do
    # ...
  end
end

说明

文件'state_machine/core'的加载将禁用Class的扩展名.也就是说,除非您调用extend StateMachine::MacroMethods,否则state_machine方法将不会在任何类中都可用.

Explanation

Loading of the file 'state_machine/core' disables extension of Class. That means that method state_machine will not be available in any class unless you call extend StateMachine::MacroMethods.

此处的详细信息: https://github.com/pluginaweek/state_machine#core-extensions

考虑使用state_machines宝石(以"s" 结尾)代替原始的state_machine宝石.

Consider using state_machines gem (ending with "s") instead of original state_machine gem.

gem 'state_machines'
gem 'state_machines-activerecord'

原始版本已不再维护(我不确定为什么),但是新版本会不时更新,这可能会防止某些错误.

The original is not maintained anymore (I am not sure why) but the new one is updated from time to time and it might prevent some errors.

这篇关于状态机Gem + Rails 4:#&lt; Class:0x007faffc93fd60&gt;的未定义方法"state_machine";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 13:37