2017-12-02 297 views
0

我有一個年齡檢查我的用戶,如果他們想註冊我的應用程序,他們必須是一定的年齡。我使用的是設計寶石,但在我的用戶模型中創建了一個方法來檢查年齡......我得到一個錯誤,指出無論我想要做什麼,都不能因爲一個零級。基本上我有一個用戶,但用戶的birth_date沒有保存。設計寶石註冊模型問題

這告訴我,我把這個邏輯放在哪裏是錯誤的地方。但我不知道在哪裏。我有一個註冊控制器和一個用戶控制器。我也有用戶模型。我沒有註冊模型,我想知道我的方法是否需要使用不同的模型,而我沒有?或者,如果我構建不正確。

我的用戶模型

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 

我的註冊模式

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 

斷點我在那裏,當我在devise_parameter_sanitizer型我得到

@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> 

回答

0

錯字。目前,它是:bith_date。將其修復爲birth_date,模型應該能夠讀取正確的屬性。

這是零,因爲你檢查self.birth_date,而您允許的參數是:bith_date

在sign_up_params允許:birth_date

+0

哦,男人......非常感謝你! – kdweber89