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

No Method Error in Statuses#show

Hi,

After adding devise and modifying the forms I am getting this error when trying to post a new status.

undefined method `first_name' for nil:NilClass

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

I suspect this is due to the first_name, last_name and user_name not making it into the database as they all are nil when I check in the rails console.

Any help is appreciated. Thanks, /Michael

7 Answers

Hello!

This is due to the fact that you are probably writing your code in Ruby 2.1.0 and Rails 4.0 .... The structure of the app is different and some safety issues were modified in order to bring up the security level.

Go to the app controller and put the code bellow in it:

before_filter :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, :remember_me) }

  end
end

See the Devise documentation to see more details: https://github.com/plataformatec/devise (Strong parameters)

Hope it will help you!

Rails console output:

User.all User Load (1.8ms) SELECT "users".* FROM "users" => #<ActiveRecord::Relation [#<User id: 2, first_name: nil, last_name: nil, profile_name: nil, email: "stjones@gmail.com", encrypted_password: "$2a$10$WhBykndZjtegghe1iVyHI.N/IjQdz0/ySUoLg70.QtGz...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 1, current_sign_in_at: "2014-03-06 19:34:39", last_sign_in_at: "2014-03-06 19:34:39", current_sign_in_ip: "192.168.1.121", last_sign_in_ip: "192.168.1.121", created_at: "2014-03-06 19:34:39", updated_at: "2014-03-06 19:34:39">]>

Hi Rodrigo,

Thanks for the help. I tried adding your code to the application_controller.rb but now I am getting this error.

syntax error, unexpected keyword_end, expecting end-of-input

Thanks again for your help!

Can you please user the markdown and insert your whole controller in here?

Seems like your just missing an "end" in your code.

here it is:

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_filter :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, :remember_me) }

  end
end

end

Take one end off and you'll be good.

Markdown is 3 back quotes not 3 quotes :)

Thanks Rodrigo! That did the trick! All works now.

Thanks Again!

You're welcome!

You're welcome! Happy to help!