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

Rick Champlin
Rick Champlin
11,072 Points

Database not storing user first_name and last_name

I'm going through the build a simple rails app and for some reason my database doesn't seem to be storing first_name and last_name for my users when they sign up. This seems to cause problems when trying to use the full_name method to pull the names into the drop down on the "New Status" page. It creates errors with "undefined method `+' for nil:NilClass. There just isn't any info in my database...

When I switch the code in _form.html.erb from

    <%= f.input :user_id, collection: User.all, label_method: :full_name %>

to

    <%= f.input :user_id, collection: User.all, label_method: :email %>

It brings up a list just fine with all the emails.

Any ideas on how to get my database to store the first_name last_name so that the full_name tag will work?

Josh Flowers
Josh Flowers
7,010 Points

I'm having the same problem

5 Answers

Brandon Barrette
Brandon Barrette
20,485 Points

So the first_name and last_name are saved by devise when a user signs up. If there is no first_name or last_name in the database, the user didn't give them when they signed up.

If you have a user that is missing this, you could just delete them and re-add them. Or use the rails console to add that information. You'd do this:

U = User.find(2) #where 2 is the id of the user
U.update_attributes( first_name: "Mike", last_name: "The Frog")
U #should now display the correct info

If there is already first_name, last_name then there is an error with your method on full_name. Also check that there isn't a typo there.

Rick Champlin
Rick Champlin
11,072 Points

I meant to put my comment as a reply to this so you would see it, but there it is below describing my error a little more in depth after your response.

Rick Champlin
Rick Champlin
11,072 Points

When I load up the project files and sign up using that and look up User.first in the rails console it shows: #<User id: 1, first_name: "Rick", last_name: "champlin", profile_name: "rick"

When I do the same thing in the one I've built, it shows: #<User id: 1, first_name: nil, last_name: nil, profile_name: nil

I've explored all of the project files and the only difference I can find is my Gemfile.

source 'https://rubygems.org'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.0.1'

# Use sqlite3 as the database for Active Record
gem 'sqlite3'
gem 'devise'

# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.0'

# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'

# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'

# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'

# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'

# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 1.2'

group :doc do
  # bundle exec rake doc:rails generates the API under doc/api.
  gem 'sdoc', require: false
end

All of my gems are newer versions. Would that have anything to do with it?

Rick Champlin
Rick Champlin
11,072 Points

I also thing the problem may have something to do with the fact that Ruby 2.0 gives me this error when I try to add the attar_accessible that they mention in "Generating Devise Views" tutorial:

attr_accessible is extracted out of Rails into a gem. Please use new recommended protection model for params(strong_parameters) or add protected_attributes to your Gemfile to use old one.

Tony McKeown
Tony McKeown
Courses Plus Student 14,300 Points

Hello Rick,

attr_accessible is considered to be a bit of a security risk so it has been replaced by strong_paramaters in rails 4.

Jason had a blog post about strong parameters.

To solve your problem in this case you should consult the devise documentation. If you scroll down to the Strong Paramaters bit you need to add something like this to the controller. In this example you are saying you can edit the username and email in the sign-up form.

```def configure_permitted_parameters devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:username, :email) } end

Having the same issue

```def configure_permitted_parameters devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:first_name, :Last_name, :degree_level, :email, :password, :password_confirmation) } devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:email, :password, :remember_me) }

end