My first time posting here!
I have a form, with a lot fields and it's working great unless any validation error occurs...
If "validates_presence_of" is raised, a "ActiveRecord::RecordInvalid in ..." page is opened and leaves my form...
How can I get errors shown on the same page/form (as always)?
All the best!
My controller
if check_cadastro == "Válido para cadastro"
code....
if @usuario.save!
session[:usuario_id] = @usuario.id
cookies[:token] = @usuario.token
render :action => "edit"
else
render "new"
end
end
MY VIEW
<%= simple_form_for @usuario do |f| %>
<%= f.error_notification %>
<div ><%= f.input :cnpj, input_html: { class: 'txt' } , :label => false %></div>
<div ><%= f.input :razaosocial, input_html: { class: 'txt' } , :label => false %></div>
<% end %>
You should call save instead of save! so the if statement can fail, your object will contain messages in its errors object, and simple_form can render them next to each form input after render :new.
Change
save!
which throws errors, to:
save
which returns false or true, with object correctly validated, so you can access errors with:
@user.errors
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With