Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

documentation for rails event handling #312

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1149,6 +1149,42 @@ to the end of your application's Rakefile in order for the above task to work:
require 'tasks/state_machine'
```

#### Rails 4 View and Controller Example

A simple example of handling events from views:-

Example show.html.erb will display all the available events for the current state

```ruby
<% @vehicle.state_events do |event| %>
<%= link_to event, vehicle_path(@vehicle, event: event), method: :patch %>
<% end %>
```

Here is a simple example of the vehicles_controller.rb using the update action to support event transitions. Authorisation would also need to be added to prevent other events or methods being triggered from the url.

```ruby
def update
event = params[:event]

if event
if @vehicle.state_events.include?(event.to_sym) and @vehicle.public_send(event)
redirect_to vehicle_path(vehicle), notice: 'Vehicle state was changed.'
else
redirect :back, notice: 'Vehicle state NOT CHANGED'
end
else
if @vehicle.update(vehicle_params)
redirect_to vehicle_path(vehicle), notice: 'Vehicle was successfully updated.'
else
render :edit
end
end
end
```



### Merb

#### Rake tasks
Expand Down