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

NoMethodError in Statuses#index

I keep on getting this error when I try to add the user name into my statuses page.

Showing /Users/javier/Projects/treebook/app/views/statuses/show.html.erb where line #5 raised:

undefined method `full_name' for nil:NilClass Extracted source (around line #5):

<p> <strong>Name: <%= @status.user.full_name %> </strong>
<p>

Rails.root: /Users/javier/Projects/treebook

Application Trace | Framework Trace | Full Trace app/views/statuses/show.html.erb:5:in `app_views_statuses_show_html_erb_1152133652498940875_70271537230720'

at first I realized when I signed in, I was using the wrong number, i used db:reset to go back to 1, but that didn't work. I then saw that in my rails console when I called User.all that under first_name and last_name there was nothing stored, but I went through a ton of troubleshooting and figured out how to get them stored in.

i have been at this for hours and I can't find a solution.

Would you be able to post your StatusesController code?

Also please add your Status and User model code.

Also, could you go to the rails console ( run "rails console" in the terminal ) and post the output of Status.all and User.all

2 Answers

sorry for the late post! i went on vacation haha.

here is my status controller

ruby
class StatusesController < ApplicationController
  before_filter :authenticate_user!, only: [:new, :create, :edit, :update]


  before_action :set_status, only: [:show, :edit, :update, :destroy]

  # 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)

    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(:name, :content)
    end
end

here is my status model

ruby
class Status < ActiveRecord::Base
    attr_accessible :content, :user_id
    belongs_to :user

    validates :content, presence: true,
                        length: { minimum: 2 }

    validates :user_id, presence: true 
end

User model

ruby
class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable, 
  #:lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me,
                    :first_name, :last_name, :profile_name


  validates :first_name, presence: true

  validates :last_name, presence: true

  validates :profile_name, presence: true,
                            uniqueness: true,
                            format: {
                              with: /\A[a-zA-Z0-9_\-]+\z/,
                              message: 'Must be formatted correctly.'
                            }



  has_many :statuses

  def full_name
    first_name + " " + last_name
  end

end

here is the output of Status.all

SELECT "statuses".* FROM "statuses" ActiveRecord::Relation Status id: 10, content: "hi", created_at: "2014-07-06 21:30:35", updated_at: "2014-07-06 21:30:35", user_id: nil>, #<Status id: 11, content: "hi", created_at: "2014-07-06 21:37:59", updated_at: "2014-07-06 21:37:59", user_id: nil>, #<Status id: 12, content: "text", created_at: "2014-07-06 21:40:59", updated_at: "2014-07-06 21:40:59", user_id: nil>, #<Status id: 13, content: "yo", created_at: "2014-07-06 21:48:13", updated_at: "2014-07-06 21:48:13", user_id: nil

and the output of User.all

SELECT "users".* FROM "users" ActiveRecord::Relation User id: 1, first_name: "javier", last_name: "nunez", profile_name: "jopara", email: "nunezcespedes@gmail.com", encrypted_password: "$2a$10$I1JCh41i5t/MTpTj9CcbcucFphKzHyksbDn1i6/inIdA...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 7, current_sign_in_at: "2014-07-07 02:14:20", last_sign_in_at: "2014-07-07 01:59:22", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", created_at: "2014-07-06 21:04:23", updated_at: "2014-07-07 02:14:20">, #<User id: 2, first_name: "familia", last_name: "nunez", profile_name: "fnunez", email: "familianunez299@gmail.com", encrypted_password: "$2a$10$oiT9bwgGJ0xkiasovDBr.eOSiIXSVLUS9V7FOQ9u8Nr7...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 1, current_sign_in_at: "2014-07-06 21:48:04", last_sign_in_at: "2014-07-06 21:48:04", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", created_at: "2014-07-06 21:48:04", updated_at: "2014-07-06 21:48:04">, #<User id: 3, first_name: "victor", last_name: "nunez", profile_name: "jopara1", email: "email@domain.com", encrypted_password: "$2a$10$RiIs1W3RVaDMBOKLtOdLu.JF27LCF1w27IiG3iKcLHKu...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 1, current_sign_in_at: "2014-07-07 05:11:40", last_sign_in_at: "2014-07-07 05:11:40", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", created_at: "2014-07-07 05:11:40", updated_at: "2014-07-07 05:11:40

It looks like you have a bunch of statuses not assigned to user (look for user_id: nil string in statuses). Re-assign them and check your test your model that you can`t make a status without user assigned.