带导轨的单模型多步骤形式 3

发布于 2024-10-12 13:21:36 字数 269 浏览 15 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

为人所爱 2024-10-19 13:21:36

我不知道正确的做法是什么,但在我的项目中,我通过向用户模型添加状态字段来做到这一点。因此,例如:

  • 状态 = 0 - 新用户
  • 状态 = 1 - 管理员拒绝了该用户
  • 状态 = 2 - 管理员要求提供更多信息

然后,您必须根据状态字段为用户生成正确的表单。您可以在控制器中执行此操作:

def multistep_form
  @user = User.find(params[:id])
  case @user.status
  when 0
    render "form_1"
  when 1
    render "rejected"
  when 2
    render "form_2"
  else
    render "error"
  end
end

或者在视图中的某个位置:

<%= render :partial => "form_{@user.status}" %>

那么当然您需要有部分:_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:

  • status = 0 - new user
  • status = 1 - admin rejected that user
  • status = 2 - admin asks for more informations

Then you have to generate correct forms for a user based on a status field. You can do it in the controller:

def multistep_form
  @user = User.find(params[:id])
  case @user.status
  when 0
    render "form_1"
  when 1
    render "rejected"
  when 2
    render "form_2"
  else
    render "error"
  end
end

Or somewhere in the view:

<%= render :partial => "form_{@user.status}" %>

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 to status field, so it won't be anything but 0, 1, and 2.

不再让梦枯萎 2024-10-19 13:21:36

我不知道这是否正是您正在寻找的,但希望它能为您指明正确的方向。

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

酒废 2024-10-19 13:21:36

如果您在用户完成步骤时开始执行逻辑,那么考虑状态机可能会更清晰。

我对 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文