Rails - 相同的模型/控制器,但不同的视图

发布于 2024-11-05 03:51:49 字数 231 浏览 1 评论 0原文

我正在构建一个表单向导,引导用户完成已创建和部署的表单。模型和控制器应该保持不变,因为唯一改变的是视图(引导用户通过每个表单字段)。完成这项任务的最佳(也是最简单的,如果可能)方法是什么?

即使通过向导,一旦用户保存表单,它就会通过相同的模型和控制器保存到相同的数据库。通过进行一些研究,似乎可以通过创建一个新视图、创建一个扩展原始控制器的简单控制器并将新控制器路由到新视图来实现这一点。

任何建议都非常感激。谢谢!

I am constructing a form wizard that guides the user through a form that has already been created and deployed. The model and controller should stay the same as the only thing of change is the view (guiding the user through each form field). What is the best (and easiest, if possible) way of accomplishing this task?

Even through the wizard, once the user saves their form, it gets saved to the same database via the same model and controller. From doing a bit research it seems that this is possible by obviously creating a new view, create a simple controller that extends the original controller, and routing the new controller to the new view.

Any suggestions are really appreciated. Thanks!

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

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

发布评论

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

评论(2

千纸鹤 2024-11-12 03:51:49

经过一些研究后,我找到了几种完成任务的方法:

  • 一种简单的方法是在表单的控制器中创建向导操作。让向导操作呈现向导的视图。
  • 更好的方法是创建一个向导控制器并让它成为表单控制器的子类。覆盖新操作并呈现向导视图。这是更优选的方法,因为 Rails 中的基本 CRUD 操作免费获取 REST。因此,遵循此方法将产生一个 RESTful 向导。

After doing some research, I figured out a couple of ways to accomplish my task:

  • A simple way is to create a wizard action within the form's controller. Have the wizard action render the wizard's view.
  • A better way is to create a wizard controller and have it subclass the form controller. Override the new action, and render the wizard view. This is the more preferred method because basic CRUD actions in Rails acquire REST for free. Thus, following this method will yield a RESTful wizard.
云朵有点甜 2024-11-12 03:51:49

您绝对应该看这里:

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

否则,简短地回答一下,您可以告诉控制器的任何操作来渲染您想要的视图。这就是基本脚手架控制器中所做的事情:

render :edit

You should definitely look here:

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

Otherwise, to answer shortly, you can tell any action of your controller to render ay view you want.That's what is done in the basic scaffold controller:

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