[英]Rails - how to validate presence of attribute AFTER creation
In my devise sign-up page, I have implemented an IP tracking feature that, when a user signs up, sends the country the user comes from, in order to populate the newly created account's attribute user_country
. 在我的设计注册页面中,我实现了IP跟踪功能,当用户注册时,它将发送用户来自的国家/地区,以填充新创建的帐户的属性user_country
。
I'd like to know if it's possible to implement a sort of validation on -user_country the same way I do for other User attributes ( email
, password
...) when the user is created, to implement a sort of validation on user_country
to be sure it's not empty nor nil, ie that a user must always have a country identified. 我想知道是否可以像创建用户时对其他User属性( email
, password
...)一样对user_country
进行某种验证,对user_country
进行某种验证以确保它不为空也不为零,即用户必须始终标识一个国家。
I can't use the classic way with validates :user_country, presence: true
, because when the user is created the user_country
is still not filled but WAITS that RegistrationController makes a call to geocoder this way => see the method on the bottom called 'after_sign_up_path_for' 我无法将经典方法与validates :user_country, presence: true
,因为创建用户时仍未填充user_country
但正在等待RegistrationController以这种方式调用地址解析器=>参见底部称为“ after_sign_up_path_for'
/app/controllers/registrations_controller.rb /app/controllers/registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
layout 'lightbox'
def update
account_update_params = devise_parameter_sanitizer.sanitize(:account_update)
# required for settings form to submit when password is left blank
if account_update_params[:password].blank?
account_update_params.delete("password")
account_update_params.delete("password_confirmation")
end
@user = User.find(current_user.id)
if @user.update(account_update_params) # Rails 4 .update introduced with same effect as .update_attributes
set_flash_message :notice, :updated
# Sign in the user bypassing validation in case his password changed
sign_in @user, :bypass => true
redirect_to after_update_path_for(@user)
else
render "edit"
end
end
# for Rails 4 Strong Parameters
def resource_params
params.require(:user).permit(:email, :password, :password_confirmation, :current_password, :user_country)
end
private :resource_params
protected
def after_sign_up_path_for(resource)
resource.update(user_country: set_location_by_ip_lookup.country) #use concerns/CountrySetter loaded by ApplicationController
root_path
end
end
Is there a way to implement a sort of 'validate' in this kind of situation? 有没有办法在这种情况下实现某种“验证”?
My understanding is you want a User
model to validate user_country
field but only during steps after the initial creation? 我的理解是,您是否希望User
模型仅在初始创建后的步骤中验证user_country
字段?
class User
validates_presence_of :user_country, on: :update
end
will only validate on update and not creation. 仅在更新时验证,而不在创建时验证。 See http://apidock.com/rails/ActiveRecord/Validations/ClassMethods/validates_presence_of for other options that you can add. 有关可添加的其他选项,请参见http://apidock.com/rails/ActiveRecord/Validations/ClassMethods/validates_presence_of 。
But for your use case, it seems odd that you need to do this in the controller after signup. 但是对于您的用例,注册后需要在控制器中执行此操作似乎很奇怪。 I think it makes more sense to always validate user_country
then inject the attribute during User creation in the controller. 我认为始终验证user_country
然后在控制器中创建User期间注入属性更有意义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.