rails:受保护的方法`around_validation'调用#<StateMachine :: Machine:0xba3014ec>

Sso*_*esS 5 ruby ruby-on-rails rails-activerecord

我正在尝试实现state_machine gem,在我的rails项目中,我安装了gem,然后我将列"state"添加到我的account_entries模型:

  def change
    add_column :account_entries, :state, :string
  end
Run Code Online (Sandbox Code Playgroud)

然后在我的account_entries模型中,我添加了状态机初始方法,如下所示:

state_machine :state, :initial => :submitted do

end
Run Code Online (Sandbox Code Playgroud)

然后在我看来我显示时间输入状态:

account_entry.state
Run Code Online (Sandbox Code Playgroud)

但是当我尝试从我的应用程序创建account_entry时,我收到此错误:

protected method `around_validation' called for #<StateMachine::Machine:0xba3014ec>
Run Code Online (Sandbox Code Playgroud)

它说它位于我的account_entries控制器的第4行,这是我的account_entries控制器的第4行.

e.account_entries.create(params.require(:account_entry).permit(:time, :account_id))
Run Code Online (Sandbox Code Playgroud)

这是一个错误吗?或者这是我的问题?我该如何解决?错误消息是什么意思?

fiv*_*git 16

这是state_machine中的一个未解决的问题.其中列出的一个修复方法是通过使around_validation方法公开来解决问题:

# config/initializers/state_machine_patch.rb
# See https://github.com/pluginaweek/state_machine/issues/251
module StateMachine
  module Integrations
     module ActiveModel
        public :around_validation
     end
  end
end
Run Code Online (Sandbox Code Playgroud)

  • 请使用forked [state_machines](https://github.com/state-machines/state_machines)gem,因为不再支持此gem.我知道这是一个很老的问题,但我只是想把它留在这里以防万一其他人遇到同样的问题. (3认同)