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 Customizing Forms Creating Relationships

creating relationships task

howing /Users/mahmudhussain/treehouse/projects/treebook/app/views/statuses/show.html.erb where line #5 raised:

undefined method `first_name' for nil:NilClass Extracted source (around line #5): 2 3 4 5 6 7 8

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

<p>

Rails.root: /Users/mahmudhussain/treehouse/projects/treebook

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

Parameters:

{"id"=>"4"} Toggle session dump Toggle env dump Response

Headers:

None

this is the error i receive? is this part of the course outdated?

firstly i didnt get the attr_accessor :email, :password, :password_confirmation, :remember_me, :first_name, :last_name, :profile_name

predefined i had to add it myself but when i do it looks like they cannot be accessed?

thanks

8 Answers

Jaden Lemmon
Jaden Lemmon
2,136 Points

I'm having the same issues on this task. It has to be that it is outdated. I just need to figure out the new way to do it.

I'm getting a similar error. When I try to do status.user.full_name it says the full_name method is undefined even though its in my user model. So I'm confused why it's doing this. It has to be because I'm using Rails 4 and the videos uses an older version , but I can't figure out how to fix this issue for the newer version.

same here. i get the same error

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

You are getting this error because things aren't saved in the database properly. Yes, this is because changes introduced in Rails 4. I regularly visit the forum to inform people about all this, because video descriptions are still not updated.

You have a few choices here:

  • learn more about Strong Parameters and try to use them in your application (it is a default in Rails 4, Rails 3 uses different way of handling parameters) - this is your best option, because Strong Parameters are likely here to stay and you will run into them sooner than later when working with Rails in the future.

  • use protected_attributes gem in your Gemfile - this should allow you to use the Rails 3 attr_accessible and related keywords as seen on the videos, but this is a temporary solution.

  • generate the whole app from scratch but using Rails 3 - this will allow you to follow the videos properly (you'd also need oilder version of Bootstrap to use styles that Jim uses in the videos) - rails _3.2.13_ new treebook - or any other version, whichever is used in the videos

I tried declaring strong params in a users controller and it's not working. I could be doing it wrong though here is my code:

private
    def user_params
      params.require(:user).permit(:user_id, :last_name, :first_name)
    end
Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

Steven,

Are you now having the problem described in the original topic, or with full_name?

My issue is just with full_name.

<% @statuses.each do |status| %>
  <div class="status">
    <strong><%= status.user.full_name %></strong> #This line here

I get an UndefinedMethod error, but the method is declared in my User.rb file. Someone suggested deleting what is in the database, but I don't know how to do it from console. I've tried googling a bit to find an answer on how to do that, but got side tracked by other treehouse videos lol :P

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

This video (around minute 2:20) shows you how to run the console and purge the statuses, although I'm not sure this is the cause of the problem. Let me know if this changed anything:

http://teamtreehouse.com/library/build-a-simple-ruby-on-rails-application/creating-an-authentication-system/migrating-statuses

Kind of...the page actually loads now because there is no full_name to attempt to display. I created a new status and it broke on my show page:

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

Same error too. So I took out full_name and put first_name, that breaks it too and it gives me the same error. Which I thought was interesting because that's making me think there is no first_name or last_name on the db table. I found this though inside my db > migrate:

class DeviseCreateUsers < ActiveRecord::Migration
  def change
    create_table(:users) do |t|
      t.string :first_name
      t.string :last_name
      t.string :profile_name

So doesn't that mean my users should have a first name and last name? Or am I thinking of this problem the wrong way?

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

OK, show me your full users controller. Something must be missing there. It's all quite hard for me, because I've actually forsaken the whole treebook project a while ago (rage quit because of those same errors), but I'm trying to help you based on my other working Rails 4 application ;)

I know what you mean, I've considered rage quitting. I'm really stubborn though and want to figure this out.

class UsersController < ApplicationController
    private
    def user_params
      params.require(:user).permit(:user_id, :last_name, :first_name)
    end
end

That's all I have in my users controller. I decided to take a peek in my form partial and just realized something. Since I followed the videos I have this:

    <%= f.inputs :user_id, :as => :select, :collection => User.all %> 
    <%= f.inputs :content %>

Even though I have a user created and signed in with a first_name and last_name I"m thinking this part of the form is broken and not actually assigning the status to that user. Idk though because the form doesn't even show up as a drop down like it's supposed to, it just has an up and down arrow.

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

OK, now show me the status controller.

class StatusesController < ApplicationController
  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
    def set_status
      @status = Status.find(params[:id])
    end

    def status_params
      params.require(:status).permit(:user_id, :content)
    end
end
Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

OK, so my guess is that it has something to do with Devise and the way it handles Strong Parameters. That's exactly the point where I gave up and that was my last thought, now I remember ;). So you may want to try and study Devise + Strong Parameters more closely. This application is "Simple" only if you use the same versions of Rails and Ruby as Jim and Jason...

Makes sense. I'll dive more into Devise and try and figure out what's wrong. Thanks for taking a look.