2017-12-02 148 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