Undefined method in Ruby

Asked

Viewed 75 times

1

I’m trying to make a relationship between two tables in Ruby, where I use the student and grades but when I go to localhost:3000/Students/1/Notes it causes me the error inserir a descrição da imagem aqui

my notes_controller.Rb:

class NotesController < ApplicationController
def index
    @student = Note.find(params[:student_id])
    @notes = @student.notes
end 

end

Notes.Rb

class Note < ApplicationRecord
 belongs_to :student end

student Rb.

class Student < ApplicationRecord
has_many :notes , dependent: :destroy

end

Any idea why he says the Notes method is undefined?

  • What is the Note method ? you are probably accessing it in a way that it does not allow to be called if it is' def self.Note 'you can access with Note.find() but if it is 'def Note' you call Note.new.find.();

  • I tried using Note.new.find(), but gave that the find method is undefined.

  • From what little I understand of Ruby, Note is a class, not a method, and you’re accessing it as if it were a method, you’re just saying in Notes that it belongs to the student (what’s not wrong too?, shouldn’t it be Student ?), and have a method to set Notes.Metodo.find(params[:student_id]), I think so will work, but what I can/know palpitate ends here.

  • Actually I had switched the balls. Replace Note.find() with Student.find and it worked. Thanks for the help

1 answer

0

Are you saying that Note belongs to Student, and trying to access in a way that he won’t allow, and will get the error response:

noMethodError in Notescontroller#index Undefined method 'Notes' for #

To resolve, as concluded in the comments, access the class Student and call her the method .find()

@student = Student.find(params[:student_id])

Browser other questions tagged

You are not signed in. Login or sign up in order to post.