有人可以举例说明如何将pluginaweek state_machine用于具有有效记录的故障单模型吗? 我不理解文档中的复杂示例。
示例状态:
答案 0 :(得分:6)
示例故障单模型(未测试)
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 %>
将所有可能的故障单转换作为链接:
<% ticket.state_transitions.each do |transition| %>
<%= link_to transition.event, ticket_path(ticket, ticket: {:state_event => transition.event}), :method => :put %>
<% end %>
列出要在控制器中过滤的所有可能转换
<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])
...