NoMethodError: 'classes' not defined for nil object

Hey guys, I’m having trouble with my Ruby on Rails app. When I try to make a new class on my Heroku app, I get an error saying the ‘classes’ method isn’t defined. What’s weird is that when I make a class in the console, it doesn’t have an id or created_at time. Here’s what my setup looks like:

class ClassesController < ApplicationController
  def index
    @class = current_student.classes.find(params[:id])
  end

  def add
    @class = current_student.classes.create
  end

  def save
    @class = current_student.classes.create(class_params)
    if @class.save
      flash[:notice] = 'Class Added'
      redirect_to class_index_path
    else
      render 'add'
    end
  end

  private

  def class_params
    params.require(:class).permit(:title)
  end
end

My School model has many classes, and the Class model belongs to a school. I’ve set up the migration for classes too. I also have a SchoolSessions controller with a current_student method. Any ideas what could be causing this? Thanks!

I’ve encountered similar issues before. It looks like the problem might be with your current_student method. Make sure it’s properly defined and returning a valid Student object. If it’s returning nil, that would explain the error you’re seeing.

Also, double-check your model associations. The Student model should have has_many :classes, and the Class model should have belongs_to :student. If these aren’t set up correctly, Rails won’t know how to connect the models.

Another thing to consider is your database setup. Run rails db:migrate to ensure all your migrations are up to date. Sometimes, Heroku can have issues if the database isn’t in sync with your local environment.

Lastly, try adding some debugging statements in your controller to see what’s actually happening. You could use Rails.logger.debug or puts statements to check the value of current_student and other variables. This might give you more insight into where things are going wrong.

Hope this helps you track down the issue!

Hey there, BrilliantCoder23! :wave:

It looks like you might be running into an issue where your current_student method is returning nil, which is why you’re seeing that error about ‘classes’ not being defined. It might be really useful to double-check that the current_student method in your SchoolSessionsController is working as expected and that the student is properly logged in.

Also, consider verifying your model associations. For example, ensure that your Student model is set up to have many classes and that your Class model correctly belongs to a student. Have you run all your migrations, especially on Heroku, to make sure everything is in sync?

I also noticed that creating a class in the console seems to behave a bit differently. Are you creating the class through a student object, such as using student.classes.create? That could explain why you’re not seeing an id or timestamps in that context.

What do you think might be happening here? Have you tried adding some debugging statements or logging to track what current_student is returning? I’d love to hear your thoughts and any further details as you work through this!

hey mate, sounds like a tricky one. have u checked if current_student is actually set? might be nil. also make sure ur associations r set up right - student should have_many :classes and class should belong_to :student. double check ur migrations ran ok too. good luck!