带导轨的单模型多步骤形式 3
我正在尝试使用 ActiveRecord 持久性执行多步骤表单。
因此,首先用户提交一个表单并创建一个“用户”。
随后,管理员会审核该“用户”,并可以 1) 拒绝该用户或 2) 要求提供更多信息。
然后生成另一个表单供用户提供新信息。
等等。
它很像一个状态机,但没有一个可用的 gem 有明确的文档。另外,我想我依赖于控制器的自定义操作。
不知道我的设计是否不好或者是否有一些正确的方法可以做到这一点?
谢谢。
I'm trying to do a multi-step form with ActiveRecord persistance.
So first the user submit a form and a "user" is created.
Later, an admin review that "user", and could 1) reject that user or 2) ask for more information.
Then another form is generated for the user to provide the new information.
And so on.
It's much like an state-machine, but none of the gems available have clear documentation. Also, I guess I'm relying on custom defined actions for the controller.
Don't know if my design is bad or if there's some correct way to do this?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不知道正确的做法是什么,但在我的项目中,我通过向用户模型添加状态字段来做到这一点。因此,例如:
然后,您必须根据状态字段为用户生成正确的表单。您可以在控制器中执行此操作:
或者在视图中的某个位置:
那么当然您需要有部分:
_form_1
、_form_2
和_form_3
。这只是示例,在实际解决方案中,您还需要保护
status
字段不被用户更改,并向status
字段添加一些验证,因此它不会是什么0、1 和 2。I don't know what is the right way of doing it, but in my project I did it by adding a status field to the user model. So, for example:
Then you have to generate correct forms for a user based on a status field. You can do it in the controller:
Or somewhere in the view:
then of course you need to have partials:
_form_1
,_form_2
, and_form_3
.This is only example, in real solution you also need to protect
status
field from being changed by user and add some validations tostatus
field, so it won't be anything but 0, 1, and 2.我不知道这是否正是您正在寻找的,但希望它能为您指明正确的方向。
Ryan Bates 在 6 月份对此做了 Railscast - http://railscasts.com/episodes/217-多步骤形式
I don't know if this is exactly what you're looking for, but hopefully it will get you pointed in the right direction.
Ryan Bates did a Railscast on this back in June - http://railscasts.com/episodes/217-multistep-forms
如果您在用户完成步骤时开始执行逻辑,那么考虑状态机可能会更清晰。
我对 http://dev.netizer 非常满意。 pl/transitions-state-machine-for-rails-3.html
然后你可以做类似
user.approve!
的事情,并连接状态机中单独意味着的操作,例如发送电子邮件。一旦每个转换获得超过 1 或两个操作,或者必须添加新的转换,建模为状态机就开始有意义了,恕我直言。
If you start doing logic as a user moves through the steps, it might be cleaner to consider a state machine.
I've been very happy with http://dev.netizer.pl/transitions-state-machine-for-rails-3.html
Then you can just do stuff like
user.approve!
And hook up the actions that means separately in the state machine, such as sending emails. As soon as you get more than 1 or two actions per transition, or have to add a new transition, it starts making sense to model as a state machine, IMHO.