フィードバックテスト済み) class Ticket < ActiveRecord::Base
attr_accessible :name, :description
attr_accessible :state_event
validates :name, :presence => true
state_machine :initial => :new do
event :accept do
transition [:new, :feedback] => :accepted
end
event :decline do
transition :new => :declined
end
event :feedback do
transition [:new, :accepted] => :feedbacked
end
event :solve do
transition [:accepted, :feedback] => :solved
end
end
end
フォーム内のすべての可能な遷移
<%= f.collection_select :state_event, @ticket.state_transitions, :event, :human_to_name, :include_blank => @ticket.human_state_name %>
は、チケットの状態をゲット:
<% ticket.state_transitions.each do |transition| %>
<%= link_to transition.event, ticket_path(ticket, ticket: {:state_event => transition.event}), :method => :put %>
<% end %>
リストコントローラ
にフィルタリングするためのすべての可能な遷移:<%= ticket.state %>
はリンクとしてすべての可能なチケットの遷移を取得します
<ul>
<li class="<%= 'active' if params[:state].blank? %>"><%= link_to 'All', tickets_path %></li>
<% Ticket.state_machine.states.each do |state| %>
<li class="<%= 'active' if params[:state] == state.name.to_s %>">
<%= link_to state.name, tickets_path(:state => state.name) %>
</li>
<% end %>
</ul>
class TicketsController extends ApplicationController
...
def index
@tickets = Ticket.where("state = ?", params[:state])
...