Handling Rails Errors with the AASM State Machine

I use the rubyist-aasm state machine to handle various states in the Event object (event is initialized, event is discussed, event is published, etc.). I added guards to prevent state changes when certain conditions are not met.

All this works fine, but it does not show any errors when the guard rejected the state change. Any idea how I can see the condition has not changed? I could check the states manually, but that sounds like an ugly solution.

aasm_state :firststate 
aasm_state :secondstate  

aasm_event :approve do
  transitions :to => :secondstate, :from => [:firststate], :guard => :has_a_price? 
end

def has_a_price?   
  self.price.present?
end
+5
source share
2 answers

SimpleStateMachine , :

def approve
  errors.add(:price, 'Invalid') if price.blank?
end
event :approve, :firststate => :secondstate

, :

validates_presence_of :price, :if => "self.second_state?"
event :approve, :firststate => :secondstate
+2

rubyist-aasm 2.0.2 add '!' , false, . , :

def approve
  @event = Event.find params[:id]

  if @event.approve!
    # transition occurred
  else
    # handle the failed transition (flash or errors)
  end
end

, ?

0

All Articles