关于nomethoderror:state_machine gem与Rails 4集成

state_machine gem integration with rails 4

我在rails 4应用程序中使用state_machine gem时遇到问题。
具有所提供的rails教程中所述的包含列调用状态的模型
http://gistflow.com/posts/679-state-machine-with-rails-basics

但是当我如下定义state_machine时:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Issue < ActiveRecord::Base
validates :title, :description, presence: true
has_many :notes

state_machine :initial => :new do
    state :new, value: 0
    state :analysed, value: 1
    state :assigned, value: 2
    state :inprogress, value: 3
    state :inreview, value: 4
    state :validation, value: 5
    state :resolved, value: 6
    state :cancelled, value: 7
    state :closed, value: 8
    state :rejected, value: 9
    state :reopened, value: 10
end

def next
    Issue.where("id > ?", self.id).first || Issue.first
end

def prev
    Issue.where("id < ?", self.id).last || Issue.last
end
end

这是我的IssueConntroller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
class IssuesController < ApplicationController

before_filter :find_issue, except: [:index, :new, :create]

def index
    @issues = Issue.all
end

def new
    @issue = Issue.new
end

def create
    @issue = Issue.new(issue_params)
    respond_to do |format|
        if @issue.save
            format.html {redirect_to @issue, :flash => { :success =>"Issue succesfully created." } }
            format.json {head :no_content}
        else
            format.html {render action: 'new'}
            format.json { render json: @issue.errors, status: :unprocessable_entity }
        end

    end
end

def show
    @note = Note.new
    @note.issue_id = @issue.id
end

def edit
end

def update
    respond_to do |format|
      if @issue.update(issue_params)
        format.html { redirect_to @issue, :flash => { :success => 'Issue was successfully updated.' }}
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @issue.errors, status: :unprocessable_entity }
      end
    end
end

def destroy
    @issue.destroy

    flash[:error]="Issue '#{@issue.title}' Deleted!"

    redirect_to issues_path
end

private
def issue_params
    params.require(:issue).permit(:title, :description)

end

def find_issue
    @issue = Issue.find(params[:id])
end

end

我在IssuesController#index中陷入#NoMethodError的未定义方法" state_machine"。
当然,我已经在gemfile中添加了gem'state_machine行,并运行bundle install命令。
看来ruby不是由Rails应用程序加载的...

谢谢您帮助我解决这个问题;)

这是迹线

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
 activerecord (4.0.0) lib/active_record/dynamic_matchers.rb:22:in `method_missing'
app/models/issue.rb:5:in `<class:Issue>'
app/models/issue.rb:1:in `<top (required)>'
activesupport (4.0.0) lib/active_support/dependencies.rb:423:in `load'
activesupport (4.0.0) lib/active_support/dependencies.rb:423:in `block in load_file'
activesupport (4.0.0) lib/active_support/dependencies.rb:615:in `new_constants_in'
activesupport (4.0.0) lib/active_support/dependencies.rb:422:in `load_file'
activesupport (4.0.0) lib/active_support/dependencies.rb:323:in `require_or_load'
activesupport (4.0.0) lib/active_support/dependencies.rb:462:in `load_missing_constant'
activesupport (4.0.0) lib/active_support/dependencies.rb:183:in `const_missing'
activesupport (4.0.0) lib/active_support/dependencies.rb:494:in `load_missing_constant'
activesupport (4.0.0) lib/active_support/dependencies.rb:183:in `const_missing'
app/controllers/issues_controller.rb:6:in `index'
actionpack (4.0.0) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (4.0.0) lib/abstract_controller/base.rb:189:in `process_action'
actionpack (4.0.0) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.0.0) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (4.0.0) lib/active_support/callbacks.rb:413:in `_run__901873103026712443__process_action__callbacks'
activesupport (4.0.0) lib/active_support/callbacks.rb:80:in `run_callbacks'
actionpack (4.0.0) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (4.0.0) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.0.0) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
activesupport (4.0.0) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.0.0) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.0.0) lib/active_support/notifications.rb:159:in `instrument'
actionpack (4.0.0) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.0.0) lib/action_controller/metal/params_wrapper.rb:245:in `process_action'
activerecord (4.0.0) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.0.0) lib/abstract_controller/base.rb:136:in `process'
actionpack (4.0.0) lib/abstract_controller/rendering.rb:44:in `process'
actionpack (4.0.0) lib/action_controller/metal.rb:195:in `dispatch'
actionpack (4.0.0) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.0.0) lib/action_controller/metal.rb:231:in `block in action'
actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:80:in `call'
actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:80:in `dispatch'
actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:48:in `call'
actionpack (4.0.0) lib/action_dispatch/journey/router.rb:71:in `block in call'
actionpack (4.0.0) lib/action_dispatch/journey/router.rb:59:in `each'
actionpack (4.0.0) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:655:in `call'
rack (1.5.2) lib/rack/etag.rb:23:in `call'
rack (1.5.2) lib/rack/conditionalget.rb:25:in `call'
rack (1.5.2) lib/rack/head.rb:11:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/flash.rb:241:in `call'
rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/cookies.rb:486:in `call'
activerecord (4.0.0) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:626:in `call'
activerecord (4.0.0) lib/active_record/migration.rb:369:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.0.0) lib/active_support/callbacks.rb:373:in `_run__77618213725076532__call__callbacks'
activesupport (4.0.0) lib/active_support/callbacks.rb:80:in `run_callbacks'
actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.0.0) li/content_length.rb:14:in `call'
rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
/Users/sebmac/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service'
/Users/sebmac/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'
/Users/sebmac/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'b/action_dispatch/middleware/static.rb:64:in `call'
railties (4.0.0) lib/rails/engine.rb:511:in `call'
railties (4.0.0) lib/rails/application.rb:97:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
rack (1.5.2) lib/rack/content_length.rb:14:in `call'
rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
/Users/sebmac/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service'
/Users/sebmac/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'
/Users/sebmac/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'


您的代码中没有问题,请尝试在捆绑后重新加载SERVER,不要忘了添加state:stringìnteger,因为您将状态值用于模型,当然也使用相同的事件

1
2
3
    event :add_new do
      transition new: :analysed
    end

作为状态机的定义:是到另一个

的过渡状态

中的

您可以为状态机

调用某些方法

1
2
3
   Issue.first.state_name
   Issue.first.state_events
   ....

如果您仍然有问题,请尝试解释更多:)