- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我正在使用 Rails 3.2.2,带有 aasm gem,我有这样的 Document
模型:
class Document < ActiveRecord::Base
include AASM
aasm do
state :unread, :initial => true
state :read
state :closed
event :view do
transitions :to => :read, :from => [:unread]
end
event :close do
transitions :to => :closed, :from => [:read, :unread]
end
end
现在在我的控制台上:
➜ ✗ bundle exec rails c
Loading development environment (Rails 3.2.2)
irb(main):006:0> Document.create!(:title => 'test')
(0.2ms) BEGIN
SQL (0.3ms) INSERT INTO `documents` (`aasm_state`, `checklist_id`, `created_at`, `description`, `dir`, `planned_date`, `procedure_id`, `section`, `subsection`, `title`, `updated_at`) VALUES (0, NULL, '2012-06-16 20:03:18', NULL, NULL, NULL, NULL, NULL, NULL, 'test', '2012-06-16 20:03:18')
(0.4ms) COMMIT
=> #<Document id: 28, title: "test", description: nil, dir: nil, section: nil, subsection: nil, planned_date: nil, procedure_id: nil, checklist_id: nil, created_at: "2012-06-16 20:03:18", updated_at: "2012-06-16 20:03:18", aasm_state: 0>
irb(main):007:0> doc = Document.last
Document Load (0.3ms) SELECT `documents`.* FROM `documents` ORDER BY `documents`.`id` DESC LIMIT 1
=> #<Document id: 28, title: "test", description: nil, dir: nil, section: nil, subsection: nil, planned_date: nil, procedure_id: nil, checklist_id: nil, created_at: "2012-06-16 20:03:18", updated_at: "2012-06-16 20:03:18", aasm_state: 0>
irb(main):008:0> doc.view!
NoMethodError: undefined method `name' for nil:NilClass
from /Library/Ruby/Gems/1.8/gems/aasm-3.0.6/lib/aasm/supporting_classes/state.rb:15:in `=='
from /Library/Ruby/Gems/1.8/gems/aasm-3.0.6/lib/aasm/aasm.rb:143:in `aasm_state_object_for_state'
from (irb):8:in `find'
from /Library/Ruby/Gems/1.8/gems/aasm-3.0.6/lib/aasm/aasm.rb:143:in `each'
from /Library/Ruby/Gems/1.8/gems/aasm-3.0.6/lib/aasm/aasm.rb:143:in `find'
from /Library/Ruby/Gems/1.8/gems/aasm-3.0.6/lib/aasm/aasm.rb:143:in `aasm_state_object_for_state'
from /Library/Ruby/Gems/1.8/gems/aasm-3.0.6/lib/aasm/aasm.rb:158:in `aasm_fire_event'
from /Library/Ruby/Gems/1.8/gems/aasm-3.0.6/lib/aasm/base.rb:48:in `view!'
from (irb):8
如你所见,我不断得到
undefined method `name' for nil:NilClass
我正在使用 Ruby 1.8.7。
最佳答案
我刚刚遇到了同样的问题。它的出现是因为默认情况下状态变量是 nil
而不是被设置为初始状态。要解决此问题,在您的情况下,您需要在模型上添加一个访问器,如下所示:
def aasm_state
self[:aasm_state] || "unread"
end
关于ruby-on-rails - AASM 不适用于我的 rails 3 和 ruby 1.8.7(未定义方法 `name' 为 nil :NilClass ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11066659/
假设我有这个类(直接取自 aasm 文档): class Job true state :running state :cleaning event :run do
我正在使用 AASM 来管理状态,并且只是想要一些列来跟踪状态更改的时间,但回调似乎不起作用。当然,问题可能出在我的方法上,我只是不确定。 aasm_state :active, :a
我正在使用AASM。根据条件,是否可能有2种不同的状态?例如: aasm_event :completes do transitions :to => condition? ? :complete
我正在使用 aasm (以前是 acts_as_state_machine)我的 Rails 4 应用程序中的 gem。我的 Post 模型上有这样的东西 ... aasm column: :
我正在使用 AASM .是否可以从任何状态转换?例如: aasm_event :publish do transitions :to => :publish, :from => ANY_STATE
我在 Rails 中有一个现有模型,我想向其中添加 AASM 状态。 根据我的理解,我应该先通过迁移将一个状态列添加到我的数据库中,然后再将一些状态添加到我的 Rails 模型中。如何根据另一列中的值
如何使 aasm 事件返回 bool 值以外的值?我正在使用 aasm 2.2.0 例如有一个 MusicPlayer 模型,它在启动时随机播放一首歌曲 aasm_state :started, :a
假设有一个对象有4个状态 :new :in_process :done :verified 还有一个方法只应在对象处于大于:in_process的状态时执行 我该如何进行这项检查?我想这可能是什么 d
如何在 AASM 中调用状态更改方法? 我想在以下模型中状态转换为付费时调用 update_foo: class Foo :incomplete, :to => :paid end def
我正在使用 AASM 来管理我的用户模型中的状态。 我想通过编辑操作改变状态。 为此,我在表单中使用了 User.aasm_states_for_select 方法填充状态的选择输入。当我点击提交按钮
我一直在使用 AASM在我当前的项目中制作状态机,想知道自动调用事件并进入下一个状态的最佳方式是什么? 我正在考虑两种方法: 设置后台作业以定期检查是否满足某些条件,从而调用事件以进入下一个状态。 让
我想在每次状态转换后调用 2 个方法。现在我正在做: aasm_event :nominate_for_publishing, :before => [:set_state_last_update
我想知道这个 gem 是否提供了一种保存状态历史的方法。我可能可以通过为每个事件添加回调来做到这一点,但我想知道其他人对这种方法的看法。 最佳答案 我最近看了这个,我找到的两个解决方案是 the as
当在状态转换的成功或进入阶段调用的方法抛出错误时,捕获此错误并确保状态恢复到先前状态的最佳方法是什么。 我正在使用 AASM gem。 最佳答案 如何使用 :guard 选项来确保可以执行转换?如果
我有两个事件: event :event1, after: :event2! do transitions to: :state2, from: :state1, guard: proc {som
我正在尝试重构一个 super 模型,该模型具有相当多行与状态和转换相关的 ActsAsStateMachine 代码,我希望将其重构为模块调用 CallStates。 #in lib/CallSta
我成功地将最新的 AASM gem 集成到一个应用程序中,并使用它来创建一个向导。就我而言,我有一个模型订单 class Order :destroy named_scope :with_use
在 state_machine我曾经做过 state :cancelled do validates_presence_of :user end 如果用户不在,它会自动取消转换。 我们如何为 aa
有没有办法定义在任何情况下在进入状态(例如 has_legs? )之前检查的守卫(例如 running )? 在所有可能的回调列表中,有事件和转换的守卫,但没有事件的守卫。 我试图对模型进行条件验证。
我的问题非常简单:如何更改按钮单击时的 aasm 转换?我应该在我的观点中加入什么? 我有两个按钮:批准和拒绝。我的状态如下所示: aasm :column => 'state' do s
我是一名优秀的程序员,十分优秀!