samedi 2 décembre 2017

Devise gem registration model issue

I have an age check for my users in which if they want to sign up for my app, they have to be a certain age. I am using the devise gem, but created a method in my user model to check the age....I get an error stating that whatever I want to do they can't due to a nil class. Basically i have a user, but the birth_date on the user is not saving.

Which tells me that where I am putting this logic is in the wrong place. However I'm not sure where. I have a registration controller, and a user controller. I also have user model. I don't have a registration model, i'm wondering if either my method needs to be in a different model that I don't have? Or if i'm building it incorrectly.

My user model

class User < ActiveRecord::Base
  before_create :age_restriction

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  def age_restriction
    if (self.birth_date&.to_date + 18.years) < Date.today # assuming dob format is mm/dd/yy
      errors.add :birth_date, 'must be older than 18'
    end
  end
end

My registration model

Cass RegistrationsController < Devise::RegistrationsController
  before_action :configure_sign_up_params, only: :create
  before_action :configure_account_update_params, only: :update

  protected

  def configure_sign_up_params
    devise_parameter_sanitizer.permit(:sign_up, keys: [:bith_date, :first_name, :last_name])
    binding.pry
  end

  def configure_account_update_params
    devise_parameter_sanitizer.permit(:account_update, keys: [:birth_date, :first_name, :last_name])
  end
end

The breakpoint I have in there, when I type in devise_parameter_sanitizer I get

 @permitted=
  {:sign_in=>[:email, :password, :remember_me],
   :sign_up=>
    [:email, :password, :password_confirmation, :bith_date, :first_name, :last_name],
   :account_update=>[:email, :password, :password_confirmation, :current_password]},
 @resource_name=:user>

Aucun commentaire:

Enregistrer un commentaire