I am using Devise, I want to bring a data from the user registration form to later make a query in the DB to see if it exists, when executing the validation I never receive the value of params[:contract]. Here is the code of my controller (the one in charge of validation):
class RegistrationsController < Devise::RegistrationsController
def create
validate_code
end
private
def validate_code
# Agrega tu lógica de validación de código aquí.
if !Contrato.exists?(codigo: params[:contrato])
flash[:notice] = 'El código de contrato no existe'
redirect_to '/users/sign_up'
else
@cliente = User.new(usuario_params)
@cliente.save
flash[:notice] = 'Usuario creado exitosamente'
redirect_to '/'
end
end
def usuario_params
params.require(:user).permit(:nombre,:apellido,:cargo,:telefono,:direccion,:contrato,:compania,:fluido,:solido,:ambiente,:email,:password,:password_confirmation,:user_type)
end
end
and this is a portion of the registration form (new.html.erb)
<div class="field">
<%= f.label :contrato %><br />
<%= f.text_field :contrato, autofocus: true, autocomplete: "contrato" %>
</div>
As can be seen in the image, if we locate ourselves in the console, in the query to be more specific, there is not the value that it should receive in params[:contract]
Any suggestion?
I think it should be:
It is used
usuario_params
to use the method you have defined at the end of your controller.which in turn is created to use strong parameters , which is the way rails has to validate the parameters to come in a request.
Perhaps for this particular case it doesn't make much sense, but when you call something like
User.new(params[:user])
, an illegal parameter could come in the request, but nevertheless exists in your modelUser
(for example, if it existed, aadmin: true
).Now, the same with strong parameters would be
User.new(usuario_params)
, where it will only add the previously allowed/required request parameters.For the purposes of your problem, you could do:
and it would be exactly the same.