我想知道你们如何使用 Controller 中的工作流或 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
感觉不对,有什么更好的解决方案?
最佳答案
我通常定义多个 Action 来处理从一种状态到另一种状态的转换,并具有明确的名称。在您的情况下,我建议您添加 publish
行动:
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
现在您的路线准确地反射(reflect)了故事发生的情况:
/stories/1234/publish
关于ruby-on-rails - 工作流或 AASM 等 gem 的最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6581565/