0

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 %>

1 Answer 1

1

Use this:

<%= error_messages_for :auteur %>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.