使用'工作流宝石在轨道'

时间:2014-03-08 06:40:49

标签: ruby-on-rails ruby-on-rails-4

我正在使用Workflow gem来集成我的模型的工作流程。

class Event < ActiveRecord::Base
......
include Workflow
  workflow do
     state :new do
      event :submit, :transitions_to => :awaiting_review
     end         
    state :awaiting_reviewed do
      event :accept, :transitions_to => :accepted
      event :reject, :transitions_to => :rejected
    end
    state :accepted
    state :rejected
  end
end

我无法使其正常工作。

这是我在控制器中创建的,当我提交时会触发。

def create
    @event = Event.new(event_params)
     @event.user_id = current_user.id
  if signed_in?
    respond_to do |format|
      if @event.save   #well! It seems I couldn't save it! 
      #debugger
        format.html { redirect_to @event, notice: 'Event was successfully created.' }
        format.json { render action: 'show', status: :created, location: @event }
      else
        format.html { render action: 'new' }
        format.json { render json: @event.errors, status: :unprocessable_entity }
      end
end
   else 
       redirect_to signin_path
       flash[:notice] = 'Please signin first'
   end
  end

抛出错误

"ActiveModel::MissingAttributeError in EventsController#create"

can't write unknown attribute `workflow_state'

模型名称有问题吗?不幸的是我的模特本身就是'事件'。工作流也在内部使用事件。这是一个问题吗?

如果您需要更多信息,请编辑帖子。

2 个答案:

答案 0 :(得分:2)

也许你还没有完全阅读README。您需要手动将列添加到事件表中。

  

与ActiveRecord集成

     

工作流库可以完全自动处理状态持久性。您只需要在名为 workflow_state 的表上定义字符串字段,并像往常一样在工程类组中包含工作流mixin:

class Order < ActiveRecord::Base
  include Workflow
  workflow do
    # list states and transitions here
  end
end

答案 1 :(得分:2)

您需要使用迁移向事件的架构添加“ workflow_state ”列。 ' workflow_state '是工作流gem工作的默认列。


或者您可以定义自定义列以保存状态并在模型中定义状态。
例如:'状态'是您的自定义列,然后代码将是

class Event < ActiveRecord::Base
  include Workflow
  workflow_column :status

  # rest of code......
  .....................
end

来源:https://github.com/geekq/workflow#custom-workflow-database-column