[英]Rails : date_select validation
I am modifying a text_field
to a date_select
in a form on my company website. 我正在公司网站上的表单
date_select
text_field
修改为date_select
。 I'm new to RoR and I have an error saying that born_on
cannot be empty. 我是RoR的新手,但我
born_on
了一个错误,说born_on
不能为空。 Here is the new code 这是新代码
<div class="lobbyForm-group">
<%= f.label :born_on, class: 'lobbyForm-label' %>
<%= f.date_select 'user', :born_on, {:order => [:day, :month, :year], :start_year => 2001, :end_year => 1985, :prompt => true, :selected => Date.today}, {:class => "lobbyForm-control lobbyForm-control-selectDate"} %>
</div>
It is validated by this line in my user.rb 通过我的user.rb中的这一行进行了验证
validates :born_on, presence: true, if: :mentee?
And I have this output in the log when I click validate : 当我单击验证时,我在日志中有此输出:
"born_on(3i)"=>"3", "born_on(2i)"=>"12", "born_on(1i)"=>"2001"
Here is the related code in the controller : 这是控制器中的相关代码:
protected
def after_sign_up_path
new_lobby_mentee_registrations_profile_path
end
def user_params
permitted_params = [:first_name, :last_name, :email, :password, :password_confirmation, :born_on, :marketing_communications_accepted]
params.require(:user).permit(*permitted_params)
params[:user].merge mentee: true
end
def ensure_required_user_information
if current_user.profile.blank?
redirect_to new_lobby_mentee_registrations_profile_path
end
end
end
What am I missing ? 我想念什么? Thanks for the help.
谢谢您的帮助。
You have to convert the date hash
to a Date
type yourself in user_params
method like this: 您必须自己在
user_params
方法中将日期hash
转换为Date
类型,如下所示:
date = Date.new params[:user]["born_on(1i)"].to_i, params[:user]["born_on(2i)"].to_i, params[:user]["born_on(3i)"].to_i
params[:user][:born_on] = date
permitted_params = [:first_name, :last_name, :email, :password, :password_confirmation, :born_on, :marketing_communications_accepted]
params.require(:user).permit(*permitted_params)
params[:user].merge mentee: true
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.