Home>

I am creating an application to enter 1 to 6 Pokemon data and register it as a party (group). Enter the required data and press the submit button to register the data.
To use two modelsfields_forUse the.

When I enter the required data, the following error occurs. I want to resolve this error.

▼ Development environment
Rails 5.2.4.3
Mac OS

Error message Applicable/related source code

▼ _party.html.haml

= form_with model: @party, local: true, class: "party-register" do | f |
  .party
    .party__wrap
      = f.fields_for: pokemons do | pokemon |
        .party__wrap__top
          -# ------------------------ 1st animal --------------------- --- #-
          .party__wrap__top__pokemon1
            .pokemon-container
              = icon'fas','angle-double-down', class: "angle-double-down-icon"
              1st
              .pokemon-container__top
                -# ---- Pokémon ---- #-
                .pokemon-container__top__name
                  .label-name
                    Pokémon
                  .form-name
                    = pokemon.text_field: name, class: "form-name__input", maxlength: "6", type: "text", autocomplete: "off"
                -# ---- Nickname ---- #-
                .pokemon-container__top__nickname
                  .label-nickname
                    nickname
                  .form-nickname
                    = pokemon.text_field: nickname, class: "form-nickname__input", maxlength: "12", type: "text", autocomplete: "off"
                -# ---- sex ---- #-
                .pokemon-container__top__gender
                  .label-gender
                    sex
                  .form-gender
                    % select {name: "exp_gender", type: "text", class: "form-gender__input", id: "exp_gender"}% option {value: ""} ----
                      % option {value: "1"} ♂
                      % option {value: "2"} ♀
                      % option {value: "3"} Unknown
------------ Omitted ------------
  .party-name
    Party name
    = f.text_field: party_name, class: "party-name__input", maxlength: "6", type: "text", autocomplete: "off"
  .register-btn
    .register-btn__text
      = f.submit "register", class: "register-btn__text__submit"

▼ parties_controller

class PartiesController

▼ pokemon.rb

class Pokemon


▼ party.rb

class Party
Supplementary information

▼ About abbreviations
cs= Current statusCurrent status
bs= Base statusRace value
ev= Effort valueEffort value

▼ Reference article/question
I want to save data in multiple models
How to use fields_for well

▼ Question history
[Rails] ActionController :: ParameterMissing is displayed and data is not saved
[Rails] form_with submit button does not respond
[Rails] Data is not saved due to ActiveRecord :: NotNullViolation error
[Rails] I want to use fields_for with form_with, but an error occurs and the page cannot be displayed.
[Rails] I want to register data to multiple models with one form_with

▼ Results of hypothesis and verification work
.require (: party)If i erase the description ofparty_nameIs saved, but no Pokemon information is saved ...

I took a peek at the verification toolnameOridTurned out to be different from that of the controller. This area is suspicious. (We also accept revisions to the view)
Stop thinking= pokemon.text_field: nameI am doing something like that, so I would appreciate it if you could teach me.

If i notice anything else, I would like to hear your opinions.

  • Answer # 1

    @ party.pokemon.buildDoes not cause an error ???
    @ party.pokemons.buildWhat will happen