Rails 3 - 处理文本输入以创建多个模型

发布于 2024-11-14 15:19:31 字数 563 浏览 7 评论 0原文

在我正在开发的应用程序中,课程有许多问题,而这些问题又具有许多步骤。现在有一个表单可以将问题添加到课程(然后可以将步骤添加到这些问题中)。我们想要的是一个只有 LaTeX 输入字段的表单,然后处理 TeX 以通过其步骤创建多个问题。

目前,我们正在问题控制器中完成这一切。我们有两个方法,texnew,它与 new 相同,只是它有一个不同的视图,重定向到另一个新方法:texcreate,它使用辅助方法来提取问题和步骤(使用一系列正则表达式),尝试创建它们,并在出现问题时闪烁一些信息丰富的错误消息。

问题是,我一直读到我们真的不应该在控制器中做很多事情,而我们应该倾向于在模型中做事情。虚拟属性可能是接受文本字段并处理它以创建单个问题的正确想法,但我无法弄清楚如何使其适用于多个问题,或者如果出现问题如何生成任何类型的错误消息一路上某个地方出错了。

有没有更好/更惯用的方法来做到这一点?

In the app I'm working on, Courses have many Problems, which in turn have many Steps. Right now there is a form for adding Problems to Courses (and then Steps can be added to those problems). What we want is to have a form that just has a field for LaTeX input, and then process the TeX to create multiple problems with their steps.

At the moment, we're doing this all in the Problems controller. We have two methods, texnew which is identical to new except it has a different view that redirects to the other new method: texcreate, which uses helper methods to extract the problems and steps (using a series of regexes), tries to create them, and flashes somewhat informative error messages if something goes wrong.

The thing is, I keep on reading that we're really not supposed to be doing a bunch of stuff in the controller, and we should favor doing things in the model instead. Virtual attributes might be the right idea for taking in a text field and processing it to create a single problem, but I can't figure out how to make it work for multiple problems, or how to generate any sort of error messages if something goes wrong somewhere along the way.

Is there some better/more idiomatic way to do this?

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

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

发布评论

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

评论(1

羁客 2024-11-21 15:19:31

如果您的所有关系都设置正确,那么您实际上并不需要虚拟属性。您可以使用新的 Rails3 嵌套属性。有一篇关于它们的好文章 在这里。这将使您更多地依赖模型验证逻辑,并保持 Rails 鼓励的精益控制器胖模型习惯。

You don't really need virtual attributes for this if all your relationships are setup properly. You can use the new rails3 nested attributes. There is a good article on them here. This will allow you to rely more on model validation logic and keep the lean controller fat model idiom that rails encourages.

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