在 Rails 3 中为 has_many :through 关系创建控制器和视图
有很多教程向您展示如何在 Rails 中为 has_many :through 关系创建模型指令,但似乎没有很多文章与设置表单以创建和编辑这些关系的过程相关。我正在寻求一些帮助(或好的例子),了解如何创建一个界面,允许用户在 Rails 应用程序中管理这些类型的关系。
这是场景:
我有用户、关系和运动员。一个用户可以有一个 与担任各种角色的运动员的关系:教练、导师、 家长或粉丝。
这是我的模型:
class User < ActiveRecord::Base
has_many :relationships
has_many :athletes, :through => :relationships
end
class Athlete < ActiveRecord :: Base
has_many :relationships
has_many :users, :through => :relationships
end
class Relationship < ActiveRecord :: Base
belongs_to :users
belongs_to :athletes
end
因此,下一步是构建视图和控制器,使我能够创建用户与运动员的关系(与教练、家长等角色)、编辑关系或销毁关系。
最终,我的目标是实现一个用户可以创建运动员并选择关联关系的场景。
不幸的是,除了模型说明或 has_many 关系的示例之外,我找不到任何具体的教程或参考资料。
如果有人有可以简单地解决此问题的链接或示例,我应该能够自定义其余部分。
There are many tutorials that show you how to create the model instructions for a has_many :through relationship in Rails, but there doesn't seem to be many articles related to the process of setting up forms to create and edit these relationships. I am seeking some assistance (or good examples) of how to create an interface that will allow users to manage these types of relationships in a Rails app.
Here's the scenario:
I have Users, Relationships, and Athletes. A User can have a
Relationship with an Athlete in a variety of roles: Coach, Mentor,
Parent, or Fan.
Here are my models:
class User < ActiveRecord::Base
has_many :relationships
has_many :athletes, :through => :relationships
end
class Athlete < ActiveRecord :: Base
has_many :relationships
has_many :users, :through => :relationships
end
class Relationship < ActiveRecord :: Base
belongs_to :users
belongs_to :athletes
end
So, the next step is to build the views and controllers that allows me to create a User-to-Athlete relationship (with a coach, parent, etc role), edit the relationship, or destroy the relationship.
Ultimately, my aim is to have a scenario where Users can create Athletes and choose the associated Relationship.
Unfortunately, I can't find any specific tutorials or references that gives me much more than the model instructions or the example for a has_many relationship.
If anyone has a link or example that can solve this problem at a simple level, I should be able to customize the rest.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
用户和运动员模型之间的关系本质上是一个
has_and_belongs_to_many
关系 (HABTM)。通过与您反复讨论,您似乎对建立这些关系的最佳方式感到困惑。一个开始阅读的好地方是 ActiveRecord 关联的文档,特别是 HABTM 关系的文档。
你的模型设置没问题。现在您已经设置了 HABTM 关系,您可以执行以下操作。假设您的 Athlete 模型和 User 模型都非常简单,除了
name
属性(一个字符串)之外什么都没有。您现在可以执行如下代码(这是 Rails 控制台的控制台输出):上面的行将创建一个名为 Mike 的用户,并自动创建一个具有适当属性的关系条目以链接两者。现在,如果您这样称呼:
现在,如果您希望允许用户在创建运动员时决定自己与运动员之间的关系,您可以将您的关系类设置为具有类型为
relation
的字段string
,在创建关系时(如我上面所示),您可以执行以下操作:希望这比我原来的答案更有帮助。如果您有任何疑问,请告诉我。并且一定要查看我上面提到的 ActiveRecord Associations 文档。
The relationship you have here between your User and Athlete model is essentially a
has_and_belongs_to_many
relationship (HABTM). By going back and forth with you it seems you're confused about what the best way to create these relationships would be.A good place to start reading would be in the documentation for ActiveRecord's Associations, specifically the documentation for HABTM relationships.
Your model setup is fine. Now that you have your HABTM relationship setup, here's what you can do. Let's assume both your Athlete and User model are very simple and have nothing but a
name
attribute, which is a string. You can now do code like this (this is console output from the rails console):The line above will create a user with name Mike, and automatically create a relationship entry with the appropriate attributes to link the two. So now if you call this:
Now if you wanted to allow the user to dictate what the relationship is between themselves and an Athlete upon Athlete creation, you could setup your Relationship class to have a
relation
field of typestring
, and when creating the relationships (as I just displayed above), you can then do something like this:Hopefully this is more helpful than my original answer. Let me know if you have any questions. And definitely be sure to look at the ActiveRecord Associations documentation I mentioned above.
尝试railscasts 或asciicasts。这就是我通常开始的地方。不确定这是否是您想要的,但这些网站上有一个关于嵌套表单的教程。我认为它是在复杂的形式下。无论如何,值得阅读/观看。
Try railscasts or ascii casts. That's where I usually start. Not sure if this is what you're after but there's a tutorial on those sites for nested forms. I think it's under complex forms. Would e worth reading / watching anyway.