如何在 ruby on Rails 中创建向导表单
我试图了解在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您应该以多步骤形式观看此 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!
有几个 插件,在 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.
Wicked 看起来对 Rails 3 很有前途:
https://github.com/schneems/wicked
Wicked looks promising for rails 3:
https://github.com/schneems/wicked
看看 wicked,在此 Rails Cast 中进行了解释:http://railscasts.com/episodes/346-wizard-forms-with-wicked?autoplay=true
Have a look at wicked, explained in this rails cast: http://railscasts.com/episodes/346-wizard-forms-with-wicked?autoplay=true
另一种方法,特别是对于更简单的多步骤表单,是根据步骤简单地显示或隐藏单个表单的部分,这样您就不会在每个步骤中都访问数据库,而是让用户构建他的对象,直到他完成为止。准备好一个有效的实例。
这种方法强烈支持使用表单类,而不是直接使用模型(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