Rails:自动建立一对多关系的脚手架
不确定我读的是否正确,但 Scaffold 似乎不会完全建立一对多关系。 例如,如果我使用脚手架创建消息
,然后我想要对这些消息
进行注释
(一条消息
-> ;很多评论
),我必须经历并改变一切。 例如,我必须在 comment
的 new
视图中将其更改为此
<% form_for(@comment) do |f| %>
,
<% form_for([@message, @comment]) do |f| %>
然后更改操作以设置 @message
var ...除其他外。
目前这无法使用 Scaffold 自动完成,对吧?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
是的。 脚手架适用于模型和相关控制器。 它不关心或处理人际关系。
Scaffold 的主要目标是使用控制器和相关视图在模型上进行 CRUD。 就这样。 任何其他要求(例如关系)都必须手动编码。
Yes. Scaffold works for a model and related controller. It does not take care of or work with relationships.
Scaffold's primary objective is to get CRUD going on a model using a controller and related views. That's all. Any other requirement like relationships has to be coded manually.
这是事实,但是,这还不是故事的结局。 Scaffold 至少有两种替代方案,它们都工作得很好,并且自动获取类之间的关系(基于 ActiveRecord 关系指示器,如 has_many)。 其中一种替代方案是 Streamlined,另一种是 ActiveScaffold。
它们主要有助于输入系统所需的数据,而不是用户输入的数据。 例如,我将它们用于表上的管理任务,当其中一个脚手架替代方案可以很好地完成很少使用的功能时,为 CRUD 构建完整的 UI 是没有意义的。 不过,您不会想使用它们来评论消息。
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.
请注意,有像 Hobo for Rails 这样的项目,允许您将字段和关联保留在模型本身内。 你无法构建关联,但它非常接近。
你最终会因为在你背后构建更多的应用程序而付出代价。 您通常不是自行推出,而是从大量预先构建的行为中减去您需要的内容。
Note that there are projects like Hobo for Rails which allow you to keep your fields and associations within the model itself. You can't scaffold associations, but it's pretty close.
You end up paying for this sugar by having a lot more of the application built behind your back. Instead of rolling your own, you're usually subtracting out what you need from a large bank of prebuilt behaviors.
您不需要繁重的 Rails 管理框架来使一对多关系正常工作。
您可以使用脚手架来完成大部分任务。
在控制器和 _form 视图中多做一点工作就可以完成剩下的工作。
方法如下...
假设我们有一个 beers 表:
以及一个 developers 表,该表具有引用 beers 表的外键 (beer_id):
我们可以使用脚手架来创建这两个表表:
命令为每个模型创建控制器和视图。
我们需要稍微修改我们的控制器和视图,以获得为每个开发人员选择啤酒的下拉菜单:
app/views/developers/_form.html.erb
将生成的 text_field 和 label 替换为beer_id 包含以下内容:
app/controllers/developer_controller.rb
编辑控制器的新方法和编辑方法:
屏幕截图
注释
Rails 脚手架很棒。 查看它为您创建的所有文件:
您所要做的就是知道当您需要的不仅仅是基本的 CRUD 操作时要修改哪些文件。
希望有帮助。
〜莱克斯
You don't need a heavy rails admin framework to get one-to-many relationships working.
You can use scaffolding to get most of the way there.
A little more work in the controller and _form view will get you the rest of the way there.
Here's how...
Assuming we have a beers table:
And a developers table that has a foreign key (beer_id) referencing the beers table:
We can use scaffolding to create both tables:
The scaffold command creates the controllers and views for each model.
We will need to modify our controllers and views a little bit to get the dropdown menu to select a beer for each developer:
app/views/developers/_form.html.erb
Replace the generated text_field and label for beer_id with the following:
app/controllers/developer_controller.rb
Edit the controller's new and edit methods:
Screen Shots
Notes
Rails scaffolding is great. Look at all the files that it creates for you:
All you have to do is know which files to modify when you want more than basic CRUD operations.
Hope that helps.
~ Lex
脚手架就是脚手架。 当您想要表上除 CRUD 之外的任何内容(这就是脚手架的作用)时,您需要更改生成的脚手架代码,或者推出您自己的脚手架代码。
Scaffolds are scaffolds. When you want anything other than a CRUD on a table (which is what a scaffold is/does), you need to alter the generated scaffolding code, or roll your own.