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!
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
Stephanie Noble
3,389 PointsIssue 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?

Stephanie Noble
3,389 Pointsstatuses_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
26,671 PointsI 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
26,671 Points@status.user.full_name

Andre' Jones
26,671 Pointsif that doesnt work paste your user model here

Stephanie Noble
3,389 PointsSo 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
26,671 Pointsare these two lines in your user.rb
validates :first_name, presence: true
validates :last_name, presence: true

Andre' Jones
26,671 Pointsim 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.

Stephanie Noble
3,389 PointsThis 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
26,671 PointsIf 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

Stephanie Noble
3,389 PointsYeah, I've been working on this with a friend for several hours. Thanks!

Andre' Jones
26,671 PointsI 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

Andre' Jones
26,671 Pointswhich video r u on

Stephanie Noble
3,389 PointsProgramming > Build a Simple Ruby on Rails Application > Customizing Forms > Creating Relationships
Andre' Jones
26,671 PointsAndre' Jones
26,671 PointsPaste your status controller code please