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

Eddie Flores
Eddie Flores
9,110 Points

Statuses won't save user_id

Hello, after overcoming a crapload of issues in the last 2 videos with simple form, I've come to a head where I can't add a first_name because it won't save it to a status. I am still getting:

undefined method `first_name' for nil:NilClass

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

</p>

I've already deleted all statuses in the rails console. Add a new status and assign it my user_id. I know what my user_id is by going into the rails console and doing the user = User.first command and it tells you id => x

I get: SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT 1
=> # <User id: 1, first_name: "Edge", last_name: "F", profile_name: "Edge", email: "wtf@gmail.com", encrypted_password: "$2a$10$Z9B0/0OXJdsCBfY6kat7q.P5/jF/RTpyqKSDwWVdAj1z...", reset_password_token: nil, reset_password_sent_at:

nil, remember_created_at: nil, sign_in_count: 1, current_sign_in_at: "2013-10-04 20:17:26", last_sign_in_at: "2013-10-04 20:17:26", current_sign_in_ip: "66.00.000.000", last_sign_in_ip: "66.00.000.000", created_at: "2013-10-04 20:17:26", updated_at: "2013-10-04 20:17:26">

I edited the IP address and name and email... just for privacy's sake.

I've followed it the way I should.. and fixed it where I needed it. This is a tutorial that needs a MUCH advised update.

Brandon Barrette
Brandon Barrette
20,485 Points

what is the code in the page that is giving this error. Also, what is the code in the statuses_controller.

10 Answers

Taylor Jones
PLUS
Taylor Jones
Courses Plus Student 1,518 Points

Not sure if you got this to work but here's something that fixed it for me:

Add/Edit this to your statuses_controller.rb under App > Controllers

def status_params params.require(:status).permit(:name, :content, :user_id) end

Mine was already on line 72

Hope that helps you!

Thanks, this helped me. Though, it's not the end of problems to me. first_name is still nil in User table. Registering it allows to submit first, last and profile names, but in DB it's nil.

Brandon Barrette
Brandon Barrette
20,485 Points

Also, you need to create a status. If there is no status (meaning @status is nil), then @status.user will also be nil, so @status.user.first_name will be giving you that error.

Eddie Flores
Eddie Flores
9,110 Points

Brandon, I got the error TRYING to post a status. If I go back and look at the statuses, it ends up showing up. The only problem then if I try to edit the status and assign it a user, it brings the error back up. It is not assigning a user_id to a status.

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

  # GET /statuses
  # GET /statuses.json
  def index
    @statuses = Status.all
    respond_to do |format|
      format.html # index.html.erb
     format.json { render json: @statuses }
    end
  end

  # GET /statuses/1
  # GET /statuses/1.json
  def show
    #@status = Status.find(params[:id])

    #respond_to do |format|
     # format.html # show.html.erb
     #format.json { render json: @status }
    #end
  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
Brandon Barrette
Brandon Barrette
20,485 Points

Ok, so in your create and update actions, you need to add current_user before Status... meaning:

def create
    @status = current_user.statuses.new(params[:status])
end

def update
    @status = current_user.statuses.find(params[:id])
end

On the create action, that will assign the status to the user id. Then, on the update action, it will have the user_id available in the form which seems to be causing your error.

Eddie Flores
Eddie Flores
9,110 Points

Did not work. Either I didn't put it in the right place, or it just doesn't work with my configuration.

Eddie Flores
Eddie Flores
9,110 Points

I still say that this tutorial is unable to be finished without any new types of configurations. I've modified it in every possible way I can, and I've re-done this tutorial 9!!!!!! TIMES IN 2-3 months! and I've yet to get passed the Creating Relationships tutorial on my version of the program. I have yet to fix this part despite using RVM, adjusting the version of bootstrap, the version of simple form, and all other things. I think that being a paying customer we should have tutorials that are at least UP-TO-DATE. I joined to learn ruby primarily. I am still wanting to learn, but there is nothing I can do with just going through the videos without actually implementing the program on my own.

I'm frustrated.... and I am just throwing my hands in the air and giving up. I don't do that often, but I've gone through EVERY SINGLE portion and morsel of code and tried to make sense of it. It doesn't work. Something about it just doesn't work. Up until Creating Relationships I've done EVERYTHING right. And all of the sudden it just doesn't work....

Mark Hipshire
Mark Hipshire
4,708 Points

Dude, I'm stuck at the same part. This is incredibly frustrating. I just got here earlier today and can't move forward.

Mark Hipshire
Mark Hipshire
4,708 Points

Try going on to the next vid in the tutorial. It tells you to use a collection instead of a user_id, and it worked for me. Still don't understand why my previous one didn't break, but I'm glad it works now.

Mark Hipshire
Mark Hipshire
4,708 Points

Nope, index is still broken.

Brandon Barrette
Brandon Barrette
20,485 Points

Ok, i don't work for treehouse, but feel your pain. I was in your shoes this past summer. It helps to ping Jason Seifer In the meantime, can you send me the link to your code on Github? I'll clone it, then try to duplicate the error on my computer.

Are you using ruby 1.9.3 and rails 3.2.12?

Mark Hipshire
Mark Hipshire
4,708 Points

Yep, those are my versions. Here's my Github.

https://github.com/hibenmedia

Also, what do you mean by ping Jason Seifer? I've never really asked the admin's for help.

Mark Hipshire
Mark Hipshire
4,708 Points

Fudge, I guess right now it's not up to date, lemme figure out how to do that again. Sigh. I'm new at this and struggle through every step.

Mark Hipshire
Mark Hipshire
4,708 Points

Hey Eddie Flores, I saw you on the other thread. I just tried something that I think may have affected me. While Brandon certainly helped me, what I needed was more to mucky around with the files more than anything. The default user id is 0, and since there are two people in the video, he's entering 1 when he's in the relationship field. I was also entering 1, but since there was only a single person on my database, me, the counting actually started at 0 and the user_id 1 was nil or undefined. This stopped the whole program.

I don't know if this is the same issue for me, but I was wondering if the counting started at 0 like in an array when he entered 1 for his number. Anyways, I went into the console, deleted all status, and started from 0 and it worked until the next video. The next one is collections, so at some point you get an auto assigned number from then on. Try it and let me know.

Brandon Barrette
Brandon Barrette
20,485 Points

So the videos use Rails 3.2.12. Rails 4 introduces some new stuff, particularly with attr_accessible. I've stuck with Rails 3.2.12 so won't be of help.

I highly recommend using the version of Rails in the tutorials. This way you can learn rails without the frustration of a piece of code needing to be changed for rails 4. Then, when you have rails and ruby under your belt, you can upgrade to rails 4.

Eddie Flores
Eddie Flores
9,110 Points

But in a real world application I will be dealing with rails 4. For example at my job, that's what they are using. Plus, how do I install the older version without a bunch of issues? Every time I install rails it automatically installs 4.

Brandon Barrette
Brandon Barrette
20,485 Points

You have to specify the version in your Gemfile.

gem 'rails', '3.2.12'
Brandon Barrette
Brandon Barrette
20,485 Points

Ok, Mark Hipshire, you have a migration (where you changed the database) that removed the column "Name" in statuses and replaced it with a user_id. It seems you missed a step in one of the videos, hence why it is saying:

undefined method `name' for #<Status:0x00000104058048>

There is no "name" column in the database anymore. So, in your _form.html.erb, you'll need to change the

<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>

It can't say name. You could try :user_id for now. I think that's what they have you do eventually. I would find the video where you do a migration to delete "name" from the database and watch that again closely.

If something is not working, push through, and you can always download the source files (they are near the video) and compare their files with yours.

Hope that helps!

Mark Hipshire
Mark Hipshire
4,708 Points

0.o WUT?! I didn't think about that. Source files would be super easy to compare to.

Todd Nestor
Todd Nestor
10,689 Points

I had this same problem since I am using Ruby 2.1.1, so I had to get rid of the attr_accessible call that the tutorial calls for.

I then had to go into app / controllers / application_controller.rb and modify it to look like this:

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception

  before_action :configure_permitted_parameters, if: :devise_controller?

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:first_name, :last_name, :profile_name, :email, :password, :password_confirmation) }
  end  
end

Then doing the following line in app / controllers / statuses_controller.rb

def status_params 
    params.require(:status).permit(:name, :content, :user_id) 
end

worked, except since we have no :name anymore I modified it to:

def status_params 
    params.require(:status).permit(:content, :user_id) 
end

This allowed it to store the user_id. After this I still kept having problems getting the name to show up, but then I went into the rails console and realized that my user_id was 5, not 1 (having created and deleted users a few times). I deleted all the users and statuses just to be safe, registered a user, verified my user_id and then I created a status using that id. After this code we were trying to get to work worked:

<%= @status.user.first_name %>

Hope that helps some.

I wanna thank everyone on this thread. It fixed all my problems with Treebook. +1000x points