我遇到了使用我的rails 4应用程序使用state_machine gem的问题。 A具有包含列调用状态的模型,如提供的rails教程中所述 http://gistflow.com/posts/679-state-machine-with-rails-basics
但是当我按如下方式定义state_machine时:
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
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'。 当然我已经将gem'state_machine行添加到我的gemfile中并运行bundle install命令。 好像宝石没有被rails应用程序加载......
感谢您帮我解决这个问题;)
这里是跟踪
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'
答案 0 :(得分:5)
代码中没有问题,尝试在捆绑后重新加载SERVER ,不要忘记添加state:string
或ìnteger
,因为您使用了状态值,对你的模型,当然还有同样的事件
喜欢
event :add_new do
transition new: :analysed
end
作为状态机的定义:是一种状态转换到另一种状态
在rails c
中,您可以为状态机调用某种方法
Issue.first.state_name
Issue.first.state_events
....
如果你还有问题,请尝试解释更多:)