简体   繁体   中英

rails : displaying error messages on a form

i have a 'modif' action, activated by a link, that loads data from my database, then displays a form via a 'modif' view. The form is linked to a @newaut. On this form, a link leads to a 'modifvalid' action. It operates some tests on the data entered and may return an error message. In case of an error, if i redirect to 'modif' action to display the error, my data come to their original values, my changes on the form are lost, and the error is displayed. So, i render my 'modif view. But @newaut doesn't exist anymore, so i must reload it with the params data. The result is ok. Is there a way to just display the error message without reloading all the data entered on the 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 ..... then
        flash[:meserr]="xxxxx"
        inderr=1
      end
      if inderr==1 then
        @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 do |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 %>

用这个:

<%= error_messages_for :auteur %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM