Rails:脚手架自动做一对多关系 [英] Rails: Scaffold to automatically do one-to-many relationship

查看:13
本文介绍了Rails:脚手架自动做一对多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不确定我是否在阅读 this 正确,但似乎 Scaffold 不会建立一对多的关系完整的.例如,如果我使用脚手架创建 messages,然后我希望在这些 messages 上添加 comments(一个 message ->许多comments),我必须经历并改变一切.例如,我必须在 commentnew 视图中更改它

Not sure if I'm reading this right, but it seems like Scaffold will not do a one-to-many relationship in its entirety. For instance, if I create messages with scaffold and then I want comments on those messages (one message -> many comments), I have to go through and change everything. For instance, I have to change this in the comment's new view

<% form_for(@comment) do |f| %>

到这里

<% form_for([@message, @comment]) do |f| %>

然后更改 Action 以设置 @message 变量...等等.

and then change the Action to set up the @message var... amongst other things.

目前无法使用 Scaffold 自动完成,对吧?

推荐答案

这是真的,但这不是故事的结局.至少有两种替代 Scaffold 的方法,它们都能很好地工作并自动获取类之间的关系(基于你的 ActiveRecord 关系指标,如 has_many).其中一种选择是 Streamlined,另一种是 ActiveScaffold.

This is true, but, it's not the end of the story. There are at least two alternatives to Scaffold that both work quite well and automatically pick up on relationships between classes (based on your ActiveRecord relationship indicators like has_many). One of these alternatives is Streamlined and the other is ActiveScaffold.

它们主要有助于输入系统需要的非用户输入数据的数据.例如,我将它们用于表上的管理任务,其中没有必要为 CRUD 构建完整的 UI,而其中一个脚手架替代方案可以很好地完成一项很少使用的功能.不过,您不会希望将它们用于对消息进行评论.

They're mainly helpful for entering in data that your system requires that is not user entered data. For example, I use them for administrative tasks on tables where there's no point in building a complete UI for CRUD when one of the scaffold alternatives will do the job just fine for a seldom used feature. You wouldn't want to use them for comments on messages though.

这篇关于Rails:脚手架自动做一对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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