Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

simple_form not displaying error

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 %>  
like image 303
OtavioLipari Avatar asked Aug 14 '26 00:08

OtavioLipari


2 Answers

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.

like image 199
James Avatar answered Aug 16 '26 11:08

James


Change

save!

which throws errors, to:

save

which returns false or true, with object correctly validated, so you can access errors with:

@user.errors
like image 25
sites Avatar answered Aug 16 '26 10:08

sites



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!