解决方法
示例票证模型(未测试)
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]) ...
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。