【发布时间】:2020-12-13 20:58:44
【问题描述】:
我正在尝试制作一个玩家角色生成器。我有一个表格,希望能让我将技能及其值附加到角色表模型上。我做了这样的模型:
class CharacterSheet < ApplicationRecord
has_many :character_sheet_skills, dependent: :destroy
has_many :skills, through: :character_sheet_skills
belongs_to :user
accepts_nested_attributes_for :skills
end
class Skill < ApplicationRecord
has_many :character_sheet_skills, dependent: :destroy
has_many :character_sheets, through: :character_sheet_skills
attr_reader :value
end
class CharacterSheetSkill < ApplicationRecord
belongs_to :skill
belongs_to :character_sheet
end
角色表模型包含有关玩家角色的数据,技能模型包含游戏中可用的所有技能。在 CharacterSheetSkill 中,我想存储玩家为其角色选择的技能以及设置技能值的整数字段。
打开表格时,我已经在数据库中拥有完整的技能列表。我想要做的只是创建一个角色表,其中包含所有这些具有附加值的技能。我尝试在表单中使用“fields_for”,但我无法真正让它发挥作用。现在它看起来像这样:
<%= simple_form_for [@user, @sheet] do |f| %>
<%= f.input :name %>
<%= f.input :experience, readonly: true, input_html: {'data-target': 'new-character-sheet.exp', class: 'bg-transparent'} %>
...
<%= f.simple_fields_for :skills do |s| %>
<%= s.input :name %>
<%= s.input :value %>
<% end %>
<% end %>
如何制作该表格以便将字符表与 CharacterSheetSkills 一起保存?
【问题讨论】:
标签: ruby-on-rails forms simple-form nested-forms