需要一些关于链接到 Rails 中的模型视图的帮助

发布于 2024-11-08 14:57:19 字数 459 浏览 0 评论 0原文

我正在开发龙与地下城角色数据库应用程序。我有一个角色模型和几个属于具有 has_one 关联的角色的模型,例如 HitPoints、ArmorClass 等。这些都是从角色视图中构建、编辑和显示的。但是,我想创建包含属于角色模型的更多信息的新页面。我想要在视图顶部有一个链接,将用户带到设备页面,该页面上显示单独的模型,例如武器、盔甲、装备等。

这是我需要一些指导的地方。装备页面是否是属于角色的新模型,而我只需将武器、盔甲、装备等的模型加载到装备视图中?

如果是这样,这些模型是否与角色模型、设备模型或两者具有belongs_to关系?

最后,如果我这样构建:Character has_one Equipment和Equipment has_many Weapons,那么Weapons模型是否也与Character模型有belongs_to关系?

预先感谢,我希望我想要做的事情很清楚。我仍然无法谈论我想用 Rails 实现什么目标。

I'm working on a Dungeons and Dragons character database application. I've got a Character model and several models that belong to Character with a has_one association, such as HitPoints, ArmorClass and so on. These are all built, edited and displayed from the Character view. However, I want to create new pages with more information that belongs to the Character model. I want a link at the top of the view that takes the user to an Equipment page, with separate models displayed on that page, such as Weapons, Armor, Gear and so forth.

This is where I need some guidance. Would the Equipment page be a new model that belongs_to Character and I just load models for Weapon, Armor, Gear and so forth into the Equipment view?

If so, would these models have a belongs_to relationship with the Character model, the Equipment model or both?

Finally, if I build it this way: Character has_one Equipment and Equipment has_many Weapons, would the Weapons model also have a belongs_to relationship with the Character model?

Thanks in advance, I hope what I'm trying to do is clear. I'm still having trouble talking about what I want to accomplish with Rails.

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

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

发布评论

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

评论(1

离线来电— 2024-11-15 14:57:19

听起来您误解了模型的一些概念。例如,HitPointsArmorClass 不应该是与您的Character 具有has_one 关联的模型。它们只是Character的属性。所以你的模型应该看起来像这样:

class Character
  attr_accessor :name, :hit_points, :armor_class

  def initialize(name, hitpoints=10, armor_class=0)
    @name = name
    @hitpoints = hitpoints
    @armor_class = armor_class
  end
  # some other stuff, etc
end

然后你可以像这样访问它们:

fighter = Character.new("Conan", 100, 12)
puts "I am #{fighter.name} and I have #{fighter.hit_points} hitpoints."
puts fighter.armor_class  # outputs 12

另外,我想说 Equipment 模型是错误的概念。您需要的是一个 Item 模型。你提到的所有东西(武器、盔甲、装备)都只是物品。而且“角色”has_many“物品”更有意义。另外,“Item”has_many“Characters”,因为您可能会拥有多个具有多个项目的字符......因此,为了保持简单,它可能是多对多的关系。然后你的“Item”模型可能有一个“ItemType”字段,它告诉你它是否是武器,盔甲,药水等。

所以你的 Item 模型可能看起来像这样

class Item
  attr_accessor :name, :type

  def initialize(name, type)
    @name = name
    @type = type
  end
end

另一个选项,它更接近于您所说的是添加一个 ItemSet 模型。我们将此模型放在 CharacterItems 之间。这样做会让你说

  • “Character”has_one“ItemSet”
  • “ItemSet”has_many“Items”
  • Character”通过“ItemSet”有许多“Items”

所以基本上,这表明在您的“世界”中您有许多角色和物品,但每个角色都有一组独特的物品,

方式。

这就是我在上面的代码中需要添加的 模型的 has_manyhas_one 等属性我省略了,这样您就可以更容易地看到模型本身,这样您就可以弄清楚您想要如何做到这一点。你自己。

It sounds like you're misunderstanding some of the concepts around models. For example, HitPoints and ArmorClass should not be models that have a has_one association with your Character. They are just attributes of Character. So your model should look something like this instead:

class Character
  attr_accessor :name, :hit_points, :armor_class

  def initialize(name, hitpoints=10, armor_class=0)
    @name = name
    @hitpoints = hitpoints
    @armor_class = armor_class
  end
  # some other stuff, etc
end

Then you can access them like so:

fighter = Character.new("Conan", 100, 12)
puts "I am #{fighter.name} and I have #{fighter.hit_points} hitpoints."
puts fighter.armor_class  # outputs 12

Also, I'd say Equipment model is the wrong concept. What you need is an Item model. All of the things you mentioned (weapons, armor, gear) are all just items. And it makes a lot more sense that a "Character" has_many "Items". Also that "Item" has_many "Characters" since you'll presumably have multiple characters with multiple items...so it COULD be a many to many relationship to keep it simple. Then your "Item" model could have an "ItemType" field which tells you if it's a weapon, armor, potion, etc.

So your Item model could look like this

class Item
  attr_accessor :name, :type

  def initialize(name, type)
    @name = name
    @type = type
  end
end

Another option, which is closer to what you're talking about is to add an ItemSet model. We'd put this model between Character and Items. Doing it this way would let you say

  • "Character" has_one "ItemSet"
  • "ItemSet" has_many "Items"
  • Character" has many "Items" through "ItemSet"

So basically, this is showing that in your "world" you have many characters and items. But each character has a unique set of those items.

This is the way I'd do it.

In the code above you need to add the has_many, has_one, etc attributes to the models. I left it out so you could see the models themselves a little easier and so you could figure out how you want to do it yourself.

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