Redirect back to the form, keeping previously entered values

In my rails 3 application, I have perspectives and solicitors. The prospect of has_many lawyers. In my form for creating a new perspective, I have a selection box that is populated with the current list of solicitors. Also in the new search form, I have a link_to new_solicitor_path link to add a new lawyer if necessary. After the used one is delivered to the new form of the solicitor, fills it and removes the submit button, it returns to the new form. It all works.

What I would like to have is when the user redirects back to the new search form all the information that he entered in order to click on the β€œnew lawyer” link is saved. Right now, when the user finishes creating a new lawyer and is redirected back to the perspective form, all fields of the form are empty again.

Update I will try to be more clear.

User links link to create new perspective

<%= link_to "New Prospect", new_prospect_path %> 

this is due to a new action that displays a partial form

 <%=render 'form'%> 

in the form, this is a selection field that is pre-populated by current lawyers and has a link to add a new lawyer.

 <%= select_tag "prosolicitor", options_from_collection_for_select(@org.solicitors.all, "id", 'fullname', @prospect.solicitors.first.id), :include_blank => true %> <%= f.label :Primary_Solicitor %><%= link_to "(new)", new_solicitor_path%><br/> 

when the user clicks on the link of new lawyers, they are transferred to the new form of solicitors. In the controller for solicitors

 def create @ solicitor=@org.solicitors.new (params[:solicitor]) session[:return_to] ||= request.referer if @solicitor.save redirect_to session[:return_to] flash[:notice]='Solicitor saved!' else render new_solicitors_path flash[:notice]='Could not save Solicitor.' end end 

This brings me back to a new perspective form, but all the data entered by the user before losing the link to the new links is lost. Thanks to Marc's comments, I think I understand that using redirect_to is causing the problem. I'm just not sure what the solution is.

+4
source share
1 answer

Instead of redirecting, just do :new . Elements of the form must remain.

 # ... else flash[:error] = 'Could not save Solicitor.' # should include .fieldWithErrors formatting in the view render :action => 'new' end 
+3
source

Source: https://habr.com/ru/post/1395115/


All Articles