rails : displaying error messages on a form -
i have 'modif' action, activated link, loads data database, displays form via 'modif' view. form linked @newaut. on form, link leads 'modifvalid' action. operates tests on data entered , may return error message. in case of error, if redirect 'modif' action display error, data come original values, changes on form lost, , error displayed. so, render 'modif view. @newaut doesn't exist anymore, must reload params data. result ok. there way display error message without reloading data entered on form. thanks.
def modif @zgtitre="modification d'un auteur" @newaut=auteur.find(params[:id]) end def modifvalid @newaut=auteur.find(params[:auteur][:id]) @newaut[:autabr]= params[:auteur][:autabr].upcase @newaut[:autnom]= params[:auteur][:autnom] inderr=0 flash[:meserr]="" if ..... flash[:meserr]="xxxxx" inderr=1 end if inderr==1 @zgtitre="modification d'un auteur après erreur" render :modif return else @newaut.save redirect_to lister_les_auteurs_path end end <%= form_for @newaut, url: {action: "modifvalid"}, method: :post |f| %> <%= f.hidden_field :id %> <div class="field"> <%= f.label :nom %><br> <%= f.text_field :autnom %> <%= flash[:meserr] %> </div> <div class="field"> <%= f.label :nom_court %><br> <%= f.text_field :autabr %> </div> <div class="commandes"> <%= f.submit "valider", class: "commandes_liens"%> <%=link_to "abandonner", lister_les_auteurs_path, class: "commandes_liens" %> </div> <% end %>
use this:
<%= error_messages_for :auteur %>
Comments
Post a Comment