Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7774145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:18:19+00:00 2026-06-01T17:18:19+00:00

I have model TeacherLeader which keeps foreign keys for other tables. I can update

  • 0

I have model TeacherLeader which keeps foreign keys for other tables. I can update this table and i try to write tests for my controller (via rspec).

My model:

#  id         :integer         not null, primary key
#  user_id    :integer
#  teacher_id :integer
#

class TeacherLeader < ActiveRecord::Base  
  belongs_to :user
  belongs_to :teacher

  validates :teacher_id, 
              :uniqueness => { :message => "already a class-head" }
end

My controller

class TeacherLeadersController < ApplicationController
 def edit
    @teacher_leader = TeacherLeader.find( params[:id] )    
    ...
  end

  def update
    @teacher_leader = TeacherLeader.find( params[:id] )
    ...

    if ( @teacher_leader.update_attributes( params[:teacher_leader] ) )                   
      redirect_to teachers_path
      ...
    else
      redirect_to edit_teacher_leader_path
      ...
      end      
    end
  end
end

So, i have problems with writing tests for PUT. Here is example of my code for PUT method:

describe TeacherLeadersController do
  render_views

  before(:each) do    
    @teacher = Factory( :teacher )
    @teacher.user.user_role = "teacher"
    @teacher.save!

    @user = Factory( :user, :user_login => Factory.next(:user_login) )
    @user.user_role = "class_head"
    @user.save!

    @sh = Factory( :user, :user_login => Factory.next(:user_login)  )
    @sh.user_role = "school_head"
    @sh.save!
  end

  describe "PUT 'update" do
    before(:each) do
      @teacher_leader = @user.create_teacher_leader( @attr_teacher_leader )
    end

    describe "for signed-in school-heads" do
      before(:each) do
        test_sign_in( @sh )
      end

      it "should update teacher leader with correct params" do
        put :update, :id => @teacher_leader, :teacher_id => @teacher.id
        @teacher_leader.reload
        @teacher_leader.teacher_id.should  == @teacher.id
      end
    end
  end
end

When i try to run my specs i get that:

1) TeacherLeadersController PUT 'update for signed-in school-heads should update teacher leader with correct params
     Failure/Error: put :update, :id => @teacher_leader, :teacher_id => @teacher.id
     NoMethodError:
       undefined method `[]' for nil:NilClass
     # ./app/controllers/teacher_leaders_controller.rb:57:in `update'
     # ./spec/controllers/teacher_leaders_controller_spec.rb:150:in `block (4 levels) in <top (required)>'

But my application works! What do i do in tests wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T17:18:21+00:00Added an answer on June 1, 2026 at 5:18 pm

    Your controller is expecting a params[:teacher_leader] hash, but your test doesn’t supply it. Try this:

    it "should update teacher leader with correct params" do
      put :update, :id => @teacher_leader, :teacher_leader => { :teacher_id => @teacher.id }
      # ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have model with a Foreign Key to itself like this: class Concept(models.Model): name
I have model in a view like this @model List<Products> I can itereate over
I have model Foo which has field bar. The bar field should be unique,
I have model like this: scope :search_posts, lambda { |query| mega_posts.where( title LIKE ?
In my controller I have model operations that can return empty results. I've setup
developers! I can't understand next situation For Example I have model class Pg::City <
I have model called test, and test can have many tests, and should be
I have model like this: class Kaart(models.Model): name = models.CharField(max_length=200, verbose_name=Kaardi peakiri, help_text=Sisesta kaardi
I have model Account, which has n, :transfers . In the list of all
I have Model class Account: and other model called class Transactions: Now i have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.