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

NoMethodError in Devise. Undefined method `date_of_birth'

I am trying to add date_of_birth to my Devise form however, I keep getting an error message stating that the 'date_of_birth' is undefined method. See my code below. Any help will be greatly appreciated. I have been working on this all morning.

Devise/Registration/Edit

<div class="form-group"> <%= f.label :date_of_birth %> <%= f.date_select :date_of_birth, class: "form-control", :autofocus => true %> </div>

Assets/Application_Controller

class ApplicationController < ActionController::Base protect_from_forgery with: :null_session before_filter :configure_permitted_parameters, if: :devise_controller?

protected

 def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:name, :email, :password) }
    devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:name, :email, :password, :current_password, :gender, :date_of_birth, :avatar, :bio) }

end

Migration

class AddGenderToUsers < ActiveRecord::Migration def change add_column :users, :is_female, :boolean, default: false add_column :users, :date_of_birth, :datetime end end

1 Answer

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

My guess: you are allowing :date_of_birth only on edit (update), which means that when you are actually creating an account (sign_up), the birth date is set to null. You can check this in rails console by finding that user and checking whether he or she has a birthdate set. So if :date_of_birth is nil, when you go to the edit form, it can't fetch the :date_of_birth of that user and put it in the form.

Please add :date_of_birth to the :sign_up sanitizer, just like you did in the :account_update sanitizer in the configure_permitted_parameters method. Then wipe out all users from the database, create a new one and try editing the account again. It should display properly now.

Thank you again! It works now.