如何在 ruby​​ on Rails 中创建向导表单

发布于 2024-08-28 01:03:49 字数 492 浏览 12 评论 0原文

我试图了解在 ruby​​ on Rails 中实现向导表单的最佳选择。理想情况下,我希望拥有它,以便应用程序注册有一个后退和下一步按钮,允许用户逐步提交数据。

因此,在第 1 步中,他们可以填写联系信息。完成后,他们可以单击下一步并进入第 2 步填写付款信息等。如果他们犯了错误,他们可以单击“返回”并更正。有些步骤是必需的,而其他步骤则不需要,但您必须完成最后一步,将数据提交到数据库才能注册。然后,他们需要能够在完成后返回并以相同的方式填写过去的步骤。 (例如:也许如果他们单击个人资料链接,他们可以以相同的方式重新完成这些步骤,因为他们不想立即完成所有步骤。也许在他们完成注册步骤之前获得一个跳过按钮? )。我还需要验证已完成的步骤,防止它们在纠正或完成之前进入下一步。

选项 1)我注意到 ajax 已被推荐为 stackoverflow 上其他问题的选项。我遇到的唯一问题是,如果禁用 javascript,用户将无法注册。理想情况下,我希望它是 ruby​​ on Rails 原生的,但我愿意使用任何必要的东西来让它工作。

I'm trying to understand the best options for pulling off a wizard form in ruby on rails. Ideally I'd like to have it so the application signup has a back and next button that allows the user to submit data in steps.

So in step 1 they could fill out contact info. Once they are done they could click next and be on step 2 to fill out payment info, etc. If they make a mistake, they can click back and correct it. Some steps will be required, while others will not, but you do have to make it to the last step to submit the data to the database to sign up. They then need the ability to go back and fill out the past steps in the same fashion after completion. (example: perhaps if they clicked on a profile link they could recomplete the steps in the same fashion because they didn't want to complete all the steps right away. Maybe by being given a skip button before they completed the steps to sign up?). I also need validation to happen on what steps have been completed preventing them from moving onto the next step until corrected or completed.

Option 1) I've noticed that ajax has been recommended as an option in other questions on stackoverflow. The only problem I have with it is that the user would not be able to sign up if javascript was disabled. Ideally I'd like to have it be native to ruby on rails but I'm willing to work with whatever is necessary to get it to work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

肩上的翅膀 2024-09-04 01:03:49

您应该以多步骤形式观看此 Rails Cast 剧集:

http://railscasts.com/episodes/217-multistep-forms< /a>

准备好倒回并再次观看。他的速度非常快!

You should watch this rails cast episode on multi step forms:

http://railscasts.com/episodes/217-multistep-forms

Be prepared to rewind and watch again. He's very quick!

帅哥哥的热头脑 2024-09-04 01:03:49

有几个 插件,在 Rails 中提供向导构建便利。
充当向导Wizardly 似乎最受欢迎。
主要思想是:
* 第一步创建模型
* 然后在后续步骤中对其进行编辑,
* 对每个步骤应用部分验证并且
* 让模型实现某种状态机。

There are a couple of plugins that provide wizzard construction facilitation in rails.
Acts as Wizard and Wizardly seem the most popular.
The main idea is to:
* create a model in the first step
* then edit it on subsequent steps,
* applying partial validation on each step and
* having the model implement some sort of state machine.

笑,眼淚并存 2024-09-04 01:03:49

Wicked 看起来对 Rails 3 很有前途:

https://github.com/schneems/wicked

Wicked looks promising for rails 3:

https://github.com/schneems/wicked

独留℉清风醉 2024-09-04 01:03:49

另一种方法,特别是对于更简单的多步骤表单,是根据步骤简单地显示或隐藏单个表单的部分,这样您就不会在每个步骤中都访问数据库,而是让用户构建他的对象,直到他完成为止。准备好一个有效的实例。

这种方法强烈支持使用表单类,而不是直接使用模型(http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/)和你需要稍微调整错误消息渲染。

优点:只有一个数据库命中,没有持久无效实例的麻烦(不是空列,before_save 健全性检查混乱的属性),没有回调地狱

缺点:更多的 html 发送给用户,错误消息调整,需要一个构建良好的表单类优雅且非常有用

An alternative approach, for simpler multi step forms especially, is to simply show-hide parts of a single form depending on the step, this way you don't hit the database on every step but rather let the user build up his object until he's ready with a valid instance.

Such an approach strongly favors using a form class instead of working with the model directly (http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/) and you need to tweak error message rendering a bit.

Pros: only one db hit, no hassle with persisting invalid instances (not null columns, before_save sanity checks for messed up attributes), no callback hell

Cons: more html sent to user, error message tweaks, requires a well built form class to be elegant and really useful

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