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

Defining method last_name does not work.

How do I make a method in Rails 4 so it works like it should like in the treebook example?

2 Answers

The problem is strong_parameters in rails 4.

There are a bunch of forum posts here about this. Here's just one: https://teamtreehouse.com/forum/attraccessible-i-am-adding-firstname-lastname-profilename-but-it-wrecks-the-project-so-far-any-ideas

I looked through them and I added the protected_attributes gem. I still get errors though.

Well the problem is that you probably saved a user to the database before this fix, which means the last_name column is empty. So when you try to call user.last_name, you are getting an error because nothing is there.

Two fixes:

  1. Delete all users from the database in the rails console. (User.delete_all), then add them again
  2. In the rails console, add the last_name to the users in the db who don't have one.

Not the attr_accessible was removed because of security reasons. I suggest learning how to use protected_attributes if you are going to proceed with rails 4.

Wiped out my database once and it was going well until I had to make a last_name method. I tried to use that command but it is not recognized as an internal or external command.

I added this code to the application_controller.rb as it was suggested to use with the strong parameters:

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, :avatar) } end

More information about that code related to devise is found here: http://devise.plataformatec.com.br/#getting-started/strong-parameters

edited to add info on devise and the code in this comment.