Home>

I am creating an app that makes a hypothesis on a certain theme.
I'm routing hypotheses associated with a certain theme in a nested relationship,
The hypothetical update action causes an error.
I'm guessing that the controller description is incorrect, but I'm stumbling on it.
I'm sorry, but I would appreciate your professor.

ActiveRecord :: AssociationNotFoundError in GuessesController # update
Association named '2' was not found on Guess;perhaps you misspelled it?
  • Parameter when an error occurs
{"_method" =>"patch",
 "authenticity_token" =>"9gsX0/ZOLwpR +/LBLrpDQ6alhQMwyzsefjy6 + ZgtwtALEVBFVzlhyzbRKZJMCT5srUmCS1kFRPzC + cJq0VR7nw ==",
 "guess" =>{"story" =>"gess1-2 new"},
 "commit" =>"save",
 "moyatto_id" =>"1",
 "id" =>"2"}
Corresponding source code
  • Description of guesses_contoroller
def update
    @moyatto = Moyatto.find (params [: moyatto_id])
    @guesses = @ moyatto.guesses.includes (params [: id])
    binding.pry
    @guess = @ guesss.update (guess_params)
  end
  private
    def guess_params
      params.require (: guess) .permit (: story) .merge (user_id: current_user.id, moyatto_id: params [: moyatto_id])
    end
  • Routing
resources: moyattos, only: [: new ,: create ,: show ,: edit ,: update,: destroy] do
    resources: guesses, only: [: new,: create,: edit,: update]
  end
  • Association with each model
# Guess model
  belongs_to: user
  belongs_to: moyatto
#Moyatto model
  belongs_to: user
  has_one_attached: image
  has_many: guesses
#User model
  devise: database_authenticatable,: registerable,
         : recoverable,: rememberable,: validatable
  has_many: moyattos
  has_many: guesses
  • VIew edit.html.erb
<% = form_with model: [@moyatto, @guess], local: true do | f |%>
<p>Register hypothesis</p>
<% = f.text_area: story%>
<% = f.submit "Save"%>
<% end%>
What I tried
  • to guess_paramsid: params [: id]I tried merging, but the error didn't change.
Supplementary information (FW/tool version, etc.)
  • Contents of guess_params at the time of binding_pry
=>"gess1-2 new", "user_id" =>1, "moyatto_id" =>"1", "id" =>"2"} permitted: true>