传递值以构建方法并在嵌套表单视图中显示标签

时间:2017-05-02 20:38:49

标签: ruby-on-rails ruby-on-rails-5

在为新角色建立能力时,会创建6个异能(每个属性一个(STR,DEX,CON,WIS,INT,CHA),每个异能在Ability表中都有一个ID。我无法使用ability_id来保存对象。 (见字符#new controller)

在与这6个字段相关的视图表单中,我如何针对6种不同的统计数据单独标记它们?

模型

class Ability < ApplicationRecord
  has_many :character_abilities
  has_one :character
end

class CharacterAbility < ApplicationRecord
  has_many :abilities
  belongs_to :ability
  belongs_to :character
end

class Character < ApplicationRecord
  has_many :character_abilities
  belongs_to :ability

  accepts_nested_attributes_for :character_abilities
end

控制器

class CharactersController < ApplicationController
  before_action :set_character, only: [:show, :edit, :update, :destroy]

  def new
    @character = Character.new
    Ability.all.each do |ability|
      @character.character_abilities.build(ability_id: ability.id)
        #Ability Load (1.7ms)  SELECT "abilities".* FROM "abilities"
        #<Ability id: 1, name: "Strength", created_at: "2017-05-02 21:40:05", updated_at: "2017-05-02 21:40:05">, 
        #<Ability id: 2, name: "Dexterity", created_at: "2017-05-02 21:40:05", updated_at: "2017-05-02 21:40:05">, 
        #<Ability id: 3, name: "Constitution", created_at: "2017-05-02 21:40:05", updated_at: "2017-05-02 21:40:05">, 
        #<Ability id: 4, name: "Intelligence", created_at: "2017-05-02 21:40:05", updated_at: "2017-05-02 21:40:05">, 
        #<Ability id: 5, name: "Wisdom", created_at: "2017-05-02 21:40:05", updated_at: "2017-05-02 21:40:05">, 
        #<Ability id: 6, name: "Charisma", created_at: "2017-05-02 21:40:05", updated_at: "2017-05-02 21:40:05">]> 
    end
  end

  def edit
  end

  def create
    @character = Character.new(character_params)

    respond_to do |format|
      if @character.save
        format.html { redirect_to @character, notice: 'Character was successfully created.' }
        format.json { render :show, status: :created, location: @character }
      else
        format.html { render :new }
        format.json { render json: @character.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    respond_to do |format|
      if @character.update(character_params)
        format.html { redirect_to @character, notice: 'Character was successfully updated.' }
        format.json { render :show, status: :ok, location: @character }
      else
        format.html { render :edit }
        format.json { render json: @character.errors, status: :unprocessable_entity }
      end
    end
  end

  private

  def set_character
    @character = Character.find(params[:id])
  end

  def character_params
    params.require(:character).permit(:name, :experience, :archetype_id, :race_id, :age, :background_id, :class_id, :alignment_id, :speed, :health, :hitdie, character_abilities_attributes: [:id, :ability_id, :character_id, :score])
  end
end

查看

<%= form_for(character) do |f| %>

  <div class="field">
    <%= f.label :name %>
    <%= f.text_field :name %>
  </div>

  <%= f.fields_for :character_abilities do |ca| %>
    <div class="field">
      <%= ca.label :score %>
      <%= ca.number_field :score %>
      </div>
  <% end %>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

Resulting table(图片)ids 19-24

Rails控制台

Started POST "/characters" for 127.0.0.1 at 2017-05-03 00:01:11 -0400
Processing by CharactersController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"TMcpNrVvp5mlsN8iP6ndvNIlBX9WNE5KYGxYxA9oUP/2K8jsJOqw4zLTT/a7+6Vp54r4NeSLpBxaXfh269TZIw==", "character"=>{"name"=>"Tester", "experience"=>"123", "race_id"=>"2", "age"=>"123", "background_id"=>"2", "archetype_id"=>"4", "alignment_id"=>"1", "health"=>"12", "character_abilities_attributes"=>{"0"=>{"score"=>"10"}, "1"=>{"score"=>"10"}, "2"=>{"score"=>"10"}, "3"=>{"score"=>"10"}, "4"=>{"score"=>"10"}, "5"=>{"score"=>"10"}}}, "commit"=>"Create Character"}
(0.1ms)  begin transaction
Alignment Load (0.2ms)  SELECT  "alignments".* FROM "alignments" WHERE "alignments"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
Archetype Load (0.1ms)  SELECT  "archetypes".* FROM "archetypes" WHERE "archetypes"."id" = ? LIMIT ?  [["id", 4], ["LIMIT", 1]]
Background Load (0.1ms)  SELECT  "backgrounds".* FROM "backgrounds" WHERE "backgrounds"."id" = ? LIMIT ?  [["id", 2], ["LIMIT", 1]]
Race Load (0.1ms)  SELECT  "races".* FROM "races" WHERE "races"."id" = ? LIMIT ?  [["id", 2], ["LIMIT", 1]]
SQL (0.3ms)  INSERT INTO "characters" ("experience", "race_id", "age", "background_id", "archetype_id", "alignment_id", "health", "created_at", "updated_at", "name") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)  [["experience", 123], ["race_id", 2], ["age", 123], ["background_id", 2], ["archetype_id", 4], ["alignment_id", 1], ["health", 12], ["created_at", 2017-05-03 04:01:11 UTC], ["updated_at", 2017-05-03 04:01:11 UTC], ["name", "Tester"]]
SQL (0.2ms)  INSERT INTO "character_abilities" ("character_id", "score", "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["character_id", 4], ["score", 10], ["created_at", 2017-05-03 04:01:11 UTC], ["updated_at", 2017-05-03 04:01:11 UTC]]
SQL (0.1ms)  INSERT INTO "character_abilities" ("character_id", "score", "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["character_id", 4], ["score", 10], ["created_at", 2017-05-03 04:01:11 UTC], ["updated_at", 2017-05-03 04:01:11 UTC]]
SQL (0.1ms)  INSERT INTO "character_abilities" ("character_id", "score", "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["character_id", 4], ["score", 10], ["created_at", 2017-05-03 04:01:11 UTC], ["updated_at", 2017-05-03 04:01:11 UTC]]
SQL (0.2ms)  INSERT INTO "character_abilities" ("character_id", "score", "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["character_id", 4], ["score", 10], ["created_at", 2017-05-03 04:01:11 UTC], ["updated_at", 2017-05-03 04:01:11 UTC]]
SQL (0.1ms)  INSERT INTO "character_abilities" ("character_id", "score", "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["character_id", 4], ["score", 10], ["created_at", 2017-05-03 04:01:11 UTC], ["updated_at", 2017-05-03 04:01:11 UTC]]
SQL (0.1ms)  INSERT INTO "character_abilities" ("character_id", "score", "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["character_id", 4], ["score", 10], ["created_at", 2017-05-03 04:01:11 UTC], ["updated_at", 2017-05-03 04:01:11 UTC]]
(10.2ms)  commit transaction
Redirected to http://localhost:8080/characters/4
Completed 302 Found in 41ms (ActiveRecord: 11.9ms)

0 个答案:

没有答案