Rails - how to validate presence of attribute AFTER creation -
I have implemented an IP tracking feature in the preparation of my sign-up page, when a user signs up, So the country sends the user, so to know the status of the new created account user_country
to populate I know whether it is possible Is - the way I do other user attributes ( email
, password
...) when the user is being created , The user_country
To ensure the implementation of the recognition of a way that it is not empty or zero, which means that the user must always recognize a country.
I can not use the classic method with valid : user_country, presence: true
because when the user is made user_country
still be filled , But WAITS registrar registrar calls the GeoOder this way => The following is the method 'after_sign_up_path_'
/ app / controller / reg Istrations_controller.rb < / P> There is a way to verify 'Apply' Is there a way to 'in such a situation? I understand that you will not be certified on the update and will not be generated. See for other options that you can add. But in the case of your use, it seems weird that after signup you need to do this in the controller. I think that it always makes sense to validate class registration controller & lt; Wise: RegistrationsController Layout 'Lightbox' def updated account_update_params = devise_parameter_sanitizer.sanitize (: account_update) [: password]. Required for blank settings # When the password if the account_update_params are left blank form to submit? 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 form set_flash_message .update_attributes with effect only Done: notice: update # sign-in @ user, password: bypass = & gt; (: User) after_update_path_for true redirect_to (@user) and render "edit" for end-end rail 4 strong parameter #def params.require resource_params .permit (: email,: password: password_confirmation, current_password, user_country) private End Def protected resource_params after_sign_up_path_for (resource) resource.update (user_country: set_location_by_ip_lookup.country) #use concerns / CountrySetter ApplicationController root_path is loaded to the end end
user
model user_country
to validate the field, but only during the initial stages?
class user is valid_prez_sof_off: user_country, but :: expiration update
user_country
, then injector the attribute during user creation in the controller.
Comments
Post a Comment