构建多页表单以在数据库中创建和编辑模型的最佳方法是什么? [英] What is the best way to structure a multi-page form to create and edit models in a database?

查看:31
本文介绍了构建多页表单以在数据库中创建和编辑模型的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Rails 还很陌生.

I'm fairly new to Rails.

在 Rails 中设计/构建多页表单的最佳方法是什么?我希望表单在提交表单的第一页时立即在数据库中创建一个新记录,并为表单的每个后续页面更新数据库中的记录.

What is the best way to design/structure a multi-page form in Rails? I would like the form to create a new record in the database immediately when the first page of the form is submitted, and for each subsequent page of the form to update that record in the database.

我希望在第一页提交后立即创建记录的原因是,可以有一个未完成的记录的概念,用户稍后再回来完成.

The reason I want the record created immediately after the first page is submitted is so that there can be the notion of an unfinished record that the user comes back to later to finish.

创建后,我想允许用户通过直接转到该部分来编辑模型的任何部分.

Once created, I would like to allow the user to edit any part of the model by going directly to that section.

我对 Rails 有足够的了解,如果你能以最好的方式指导我在概念上构建它,我应该能够自己找出代码.

I know enough Rails where if you guide me with the best way to structure this conceptually, I should be able to figure out the code myself.

谢谢.

推荐答案

我有一个以这种方式工作的多步骤注册过程.我第一次创建记录,然后其他步骤是对该记录进行编辑/更新.我为此使用了多个控制器;与尝试将所有逻辑塞进一个控制器操作中相比,这是一种更简洁的方法(尽管您可以使用来自同一个控制器的多个操作并且它也能正常工作,但不要忘记为自定义操作创建路由).这种方法使在第一个步骤之后添加的数据的验证变得更加困难,但您始终可以通过在模型上调用 errors.add 来添加自己的错误,本质上是滚动您自己的验证.您还可以在会话控制器中编写逻辑,以在用户稍后返回且尚未完成时将其引导回多步骤表单中的同一步骤.

I have a multi-step signup process that works this way. I create the record the first time and then the other steps are edit/updates on that record. I use multiple controllers for this; it is a much cleaner approach than trying to cram all the logic into one controller action (although you could use multiple actions from the same controller and it would work just as well, but don't forget to create routes for your custom actions). This approach makes validation more difficult for the data added in steps after the first, but you can always add your own errors by calling errors.add on your model, essentially rolling your own validations. You can also write logic in your sessions controller to direct the user back to the same step in the multi-step form if they return later and have not completed it.

这篇关于构建多页表单以在数据库中创建和编辑模型的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆