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

问题描述

不知道如果我正在阅读的权利,但似乎脚手架不会做一对多的关系完整。例如,如果我使用scaffold创建 messages ,然后对这些消息 / code>(一个消息 - >许多注释),我必须通过并改变一切。例如,我必须在注释的新视图

 <%form_for(@comment)do | f | %GT; 

至此

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

然后更改Action以设置 @message $ ...

这个目前不能用Scaffold自动完成,对不对?

解决方案

这是真的,但是,这不是故事的结尾。 Scaffold至少有两种选择,它们都能很好地工作,并且自动选择类之间的关系(基于ActiveRecord关系指示器,如has_many)。其中一个替代方法是,另一个是

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| %>

to this

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

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

This cannot currently be done automatically with Scaffold, right?

解决方案

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.

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:脚手架自动做一对多的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 19:32