Ruby on rails Rails有很多:通过嵌套表单和简单表单

Ruby on rails Rails有很多:通过嵌套表单和简单表单,ruby-on-rails,forms,simple-form,nested-forms,Ruby On Rails,Forms,Simple Form,Nested Forms,我正在尝试制作一个玩家角色生成器。我有一个表单,希望它能让我将技能和它们的值附加到角色表模型上。我制作了这样的模型: class CharacterSheet < ApplicationRecord has_many :character_sheet_skills, dependent: :destroy has_many :skills, through: :character_sheet_skills belongs_to :user accepts_nested_a

我正在尝试制作一个玩家角色生成器。我有一个表单,希望它能让我将技能和它们的值附加到角色表模型上。我制作了这样的模型:

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
<%= 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 %>
类字符表
角色表模型保存玩家角色的数据,技能模型拥有游戏中所有可用的技能。在CharacterSheetSkill中,我希望存储玩家为其角色选择的技能以及设置技能值的整数字段

在打开表单时,我已经在数据库中列出了完整的技能列表。我想在表单中做的就是创建一个具有所有这些技能和附加值的角色表。我试着在表单中使用“fields_for”,但我真的无法实现。现在看起来是这样的:

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
<%= 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一起保存字符表?

这里更好的方法是使用
skills
作为标准化表,在其中存储技能的“主”定义,如名称和描述

class CharacterSheetSkill < ApplicationRecord
  belongs_to :skill
  belongs_to :character_sheet
  delegate :name, to: :skill
end
如果你想让用户选择技能,你可以使用select来代替隐藏字段

当然,除非您“播种”输入,否则不会显示任何内容:

class CharacterSheetController < ApplicationController
  def new
    @character_sheet = CharacterSheet.new do |cs|
      # this seeds the association so that the fields appear
      Skill.all.each do |skill|
        cs.character_sheet_skills.new(skill: skill)
      end
    end
  end

  def create
    @character_sheet = CharacterSheet.new(character_sheet_params)
    if @character_sheet.save
      redirect_to @character_sheet
    else
      render :new
    end
  end

  private
  def character_sheet_params
    params.require(:character_sheet)
          .permit(
             :foo, :bar, :baz,
             character_sheet_skill_attributes: [:skill_id, :value]
          )
  end
end
class CharacterSheetController
这里更好的方法是使用
技能
作为标准化表,存储技能的“主”定义,如名称和描述

class CharacterSheetSkill < ApplicationRecord
  belongs_to :skill
  belongs_to :character_sheet
  delegate :name, to: :skill
end
如果你想让用户选择技能,你可以使用select来代替隐藏字段

当然,除非您“播种”输入,否则不会显示任何内容:

class CharacterSheetController < ApplicationController
  def new
    @character_sheet = CharacterSheet.new do |cs|
      # this seeds the association so that the fields appear
      Skill.all.each do |skill|
        cs.character_sheet_skills.new(skill: skill)
      end
    end
  end

  def create
    @character_sheet = CharacterSheet.new(character_sheet_params)
    if @character_sheet.save
      redirect_to @character_sheet
    else
      render :new
    end
  end

  private
  def character_sheet_params
    params.require(:character_sheet)
          .permit(
             :foo, :bar, :baz,
             character_sheet_skill_attributes: [:skill_id, :value]
          )
  end
end
class CharacterSheetController
代码似乎完全符合我的需要!谢谢你,我可能从来没有想过在那个地方有一个委托人。代码似乎完全按照我的需要工作!谢谢你,我可能从来没有想到在那个地方会有授权人。