微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ruby-on-rails – 有人可以为pluginaweek – statemachine提供一个活跃的记录示例吗?

有人可以给出一个简单的例子,说明如何使用 pluginaweek state_machine作为具有活动记录的票证模型吗?
我不明白文档中复杂的例子.

示例说明:

>新 – >接受,拒绝,反馈
>已接受 – >解决或反馈
>反馈 – >接受或解决

解决方法

示例票证模型(未测试)

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 举报,一经查实,本站将立刻删除。

相关推荐