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

Issue with db not accepting _name parameters

Initially I was receiving the following errors when trying to assign a user_id to a status post within my application:

NoMethodError in Statuses#show

NoMethodError in Statuses#index

undefined method `full_name' for nil:NilClass

This happened while I was following along with Programming > Build a Simple Ruby on Rails Application > Customizing Forms > Creating Relationships. I found forum posts about the same issue, so I followed many of those instructions. I made sure my code matched that of the program files, recreated users, ran db:migrate, deleted the db, & recreated the db. I also watched all of the videos within the tutorial again to make sure I didn't miss any steps. All to no avail.

In Terminal running rails console user = User.first returned: <User id: 1, first_name: nil, last_name: nil, profile_name: nil . . .

The rest of the fields were accepted just fine. For some reason those _name values aren't being stored in the database when a user is created or edited, which I'm guessing is why I'm not able to use full_name.

In treebook/app/models/user.rb I replaced

def full_name
    first_name + " " + last_name
end

with

def full_name
    email
end

to make sure I could post a status at all. Switching to email instead of full_name worked & I was able to post a status. But for obvious reasons I would prefer being able to use full_name.

I've been tinkering with the code, but I honestly have no idea how to make the database accept the first_name, last_name, & profile_name values. Help?

Andre' Jones
Andre' Jones
26,671 Points

Paste your status controller code please

statuses_controller.rb:

class StatusesController < ApplicationController
  # 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
  # GET /statuses/new.json
  def new
    @status = Status.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @status }
    end
  end

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

  # POST /statuses
  # POST /statuses.json
  def create
    @status = Status.new(params[:status])

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

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

    respond_to do |format|
      if @status.update_attributes(params[:status])
        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 = Status.find(params[:id])
    @status.destroy

    respond_to do |format|
      format.html { redirect_to statuses_url }
      format.json { head :no_content }
    end
  end
end
Andre' Jones
Andre' Jones
26,671 Points

I think you should reset the database. rake db:reset then do rake db:migrate. make sure your full_name method is right and when ur calling it make sure its status.user.full_name

Andre' Jones
Andre' Jones
26,671 Points

if that doesnt work paste your user model here

So in user.rb when I switch back to

def full_name
    first_name + " " + last_name
end

which is what it's supposed to be I get this error when running the app:

NoMethodError in Statuses#index

undefined method `+' for nil:NilClass

But it references line #19 in application.html.erb, which is:

<li><%= link_to current_user.full_name, edit_user_registration_path %></li>
Andre' Jones
Andre' Jones
26,671 Points

are these two lines in your user.rb

validates :first_name, presence: true

validates :last_name, presence: true

Andre' Jones
Andre' Jones
26,671 Points

im thinking either first_name or last_name is nil which is causing the error. so cleaning the data from the data base and re -registering making sure all those fields are filled in.

This is my user.rb:

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
  # attr_accessible :title, :body

  has_many :statuses

  def full_name
    email
  end
end
Andre' Jones
Andre' Jones
26,671 Points

If you cant get it figured out. just copy your files into a zip file and email it to support they will go through it for you

Yeah, I've been working on this with a friend for several hours. Thanks!

Andre' Jones
Andre' Jones
26,671 Points

I had the same problem on that vid but its been a while

Copy the project files compare code or copy ur code to a zip file and and email it to support for help.

Sorry I couldnt be more help.

1 Answer

Programming > Build a Simple Ruby on Rails Application > Customizing Forms > Creating Relationships