2012-06-16 18 views
8

Tôi đang sử dụng Rails 3.2.2, với đá quý aasm, tôi có Document mô hình theo cách này:AASM không hoạt động với đường ray của tôi 3 và ruby ​​1.8.7 (phương thức không xác định `tên 'cho nil: NilClass)

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 

bây giờ giao diện điều khiển của tôi:

➜ ✗ 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  

Như bạn có thể thấy tôi tiếp tục nhận được

không xác định phương pháp 'tên' cho con số không: NilClass

0.123.

Tôi đang sử dụng Ruby 1.8.7.

+0

Bạn đã tạo cột aasm 'aasm_state' như thế nào? (lược đồ cơ sở dữ liệu của bạn trông như thế nào?) Có thể là bạn đã sử dụng số nguyên kiểu cột cho nó? Đảm bảo sử dụng ** chuỗi **. :) – alto

Trả lời

6

Tôi vừa gặp sự cố tương tự. Nó phát sinh bởi vì mặc định biến trạng thái là nil thay vì được đặt ở trạng thái ban đầu. Để khắc phục điều này, trong trường hợp của bạn, bạn cần phải thêm người truy cập vào mô hình, như vậy:

def aasm_state 
    self[:aasm_state] || "unread" 
end 
0

Cần viết tên chính xác của cột.

aasm column: :aasm_state do 
end 
Các vấn đề liên quan