ruby-on-rails – 工作流程或AASM等宝石的最佳实践

我想知道你们如何在控制器中使用工作流程或AASM gem,如果你想更新所有属性,还需要工作流程/ AASM回调才能正常启动.

目前,我使用它像这样:

class ModelController < ApplicationController
    def update
      @model = model.find(params[:id])

      if params[:application]['state'].present?
        if params[:application]['state'] == "published"
          @model.publish!
        end
      end
      if @model.update_attributes(params[:application]); ... end
    end
  end

感觉不对,什么是更好的解决方案?

解决方法

我通常定义多个动作来处理从一个状态到另一个状态的转换并具有明确的名称.在您的情况下,我建议您添加发布操作:
def publish
  # as the comment below states: your action 
  # will have to do some error catching and possibly
  # redirecting; this goes only to illustrate my point
  @story = Story.find(params[:id])
  if @story.may_publish?
    @story.publish!
  else
   # Throw an error as transition is not legal
  end
end

在您的routes.rb中声明:

resources :stories do
  member do
    put :publish
  end
end

现在,您的路线正好反映了故事发生的情况:/ stories / 1234 / publish

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...