1

This is a followup based on the great answers in RESTful resource for a State Machine and this question is probably more rest related than State Machine

I am using the Statesman Gem in a Rails 4.2 App. I have a Service model and an associated state_transitions model that stores the transitions through Active Record.

A transiton method is shown in the Statesman Docs as such: Order.first.state_machine.transition_to!(:cancelled)

I know this is not even close

In my case I have button_to 'ok', service_path, action: "#{service}.transition_to!(:received)"

In my Service model transition_to is delegated to the state machine

How can I submit the request to change the state through my button_to form?

Community
  • 1
  • 1
tbrooke
  • 2,137
  • 2
  • 17
  • 25

1 Answers1

3

I think that you're confusing controller actions with actions on models. They should really be two separate things - you should have a controller action that you can call from a view, and then inside that controller action, you can make your state change. For example:

routes.rb

resources :orders do
  member do
    put "receive" => "orders#receive", as: :receive
  end
end

OrdersController

...
def receive
  order = Order.find(params[:id])
  if order.state_machine.transition_to!(:received)
    flash[:notice] = "Success"
    redirect_to action: :show, id: order.id
  else
    flash[:error] = "Could not transition to 'received'"
    render action: :show, id: order.id
  end
end
...

view.rb

...
= button_to "Mark as received", receive_order_path(order), method: :put

Note that I'm writing pseudo-code off of the top of my head, but it should be more or less valid. Please excuse any minor syntax errors.

Bryce
  • 2,802
  • 1
  • 21
  • 46
  • Great Answer and answered exactly what I was asking re: the difference between a controller action and an action on a model - or how do you connect up an http or RESful request and sending messages to and from objects - This answer really helps Thanks – tbrooke Jan 26 '15 at 14:47