nk06
nk06

Reputation: 69

Working with 'Workflow Gem in rails'

I am working using Workflow gem to integrate a workflow for my model.

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

I couldn't make it work properly.

Here is my create in the controller that gets triggered when I submit.

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

It throws and error

"ActiveModel::MissingAttributeError in EventsController#create"

can't write unknown attribute `workflow_state'

Is it the problem with model name? Unfortunately my model itself is 'Event'. Workflow uses event internally too. Was that a problem?

Will edit the post if you need more information.

Upvotes: 0

Views: 1580

Answers (2)

Prashant Raghav
Prashant Raghav

Reputation: 41

you need to add a column 'workflow_state' to your schema of Event using migration. 'workflow_state'is default column on which workflow gem works.


or you can define your custom column to save the states and define that in model.
For example: 'status' is your custom column then code will be

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

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

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

Upvotes: 2

Raj
Raj

Reputation: 22926

Maybe you have not read the README completely. You need to add the columns manually by yourself to your events table.

Integration with ActiveRecord

Workflow library can handle the state persistence fully automatically. You only need to define a string field on the table called workflow_state and include the workflow mixin in your model class as usual:

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

Upvotes: 2

Related Questions