我想知道你们如何在控制器中使用工作流程或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