Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Build a Simple Ruby on Rails Application Testing the Whole App Before Filters

undefined method `statuses' for nil:NilClass

When I try to complete the tests outlined in this video I get the errors shown below. I'm using Rails 4 which has been a source of error before

any help would be appreciated!

Console Output

  1) Error:
StatusesControllerTest#test_should_create_status:
NoMethodError: undefined method `statuses' for nil:NilClass
    app/controllers/statuses_controller.rb:30:in `create'
    test/controllers/statuses_controller_test.rb:22:in `block (2 levels) in <class:StatusesControllerTest>'
    test/controllers/statuses_controller_test.rb:21:in `block in <class:StatusesControllerTest>'

7 tests, 12 assertions, 0 failures, 1 errors, 0 skips

test/controllers/statuses_controller_test.rb:

require 'test_helper'

class StatusesControllerTest < ActionController::TestCase
  setup do
    @status = statuses(:one)
  end

  test "should get index" do
    get :index
    assert_response :success
    assert_not_nil assigns(:statuses)
  end

  test "should get new" do
    get :new
    assert_response :redirect 
    assert_redirected_to new_user_session_path
  end

  test "should create status" do
    assert_difference('Status.count') do #LINE 21
      post :create, status: { content: @status.content } #LINE 22
    end

     assert_redirected_to status_path(assigns(:status))
   end

  test "should show status" do
    get :show, id: @status
    assert_response :success
  end

  test "should get edit" do
    get :edit, id: @status
    assert_response :success
  end

  test "should update status" do
    patch :update, id: @status, status: { content: @status.content}
    assert_redirected_to status_path(assigns(:status))
  end

  test "should destroy status" do
    assert_difference('Status.count', -1) do
      delete :destroy, id: @status
    end

    assert_redirected_to statuses_path
  end
end

app/controllers/statuses_controller.rb

class StatusesController < ApplicationController
  before_action :set_status, only: [:show, :edit, :update, :destroy]

  before_filter :authenticate_user!, only:[:new]
  # GET /statuses
  # GET /statuses.json
  def index
    @statuses = Status.all
  end

  # GET /statuses/1
  # GET /statuses/1.json
  def show
  end

  # GET /statuses/new
  def new
    @status = Status.new
  end

  # GET /statuses/1/edit
  def edit
  end

  # POST /statuses
  # POST /statuses.json
  #def create
  #  @status = Status.new(status_params)
  def create 
 @status = current_user.statuses.new(status_params) #LINE30

    respond_to do |format|
      if @status.save
        format.html { redirect_to @status, notice: 'Status was successfully created.' }
        format.json { render action: 'show', status: :created, location: @status }
      else
        format.html { render action: 'new' }
        format.json { render json: @status.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /statuses/1
  # PATCH/PUT /statuses/1.json
  def update
    respond_to do |format|
      if @status.update(status_params)
        format.html { redirect_to @status, notice: 'Status was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @status.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /statuses/1
  # DELETE /statuses/1.json
  def destroy
    @status.destroy
    respond_to do |format|
      format.html { redirect_to statuses_url }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_status
      @status = Status.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def status_params
      params.require(:status).permit(:content)
      #had permitted :name too
    end
end

2 Answers

Lisa Rossiter
Lisa Rossiter
3,630 Points

Ok so I am just starting out but it looks like(I may be wrong haha) that you are trying to get statuses on a user but haven't signed one in?

You might be right!

I continued onto the next videos where we start adding "sign_in users(:blah)" and the error disappeared! Not sure why it didn't show up in Jason's example, but oh well!

me too.