加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – AASM无法使用我的rails 3和ruby 1.8.7(未定义

发布时间:2020-12-16 19:11:31 所属栏目:百科 来源:网络整理
导读:我正在使用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 :
我正在使用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','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,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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读