Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect back to form keeping previously entered values

In my rails 3 app I have Prospects and Solicitors. Prospects has_many solicitors. In my form to create a new prospect I have a select box that is per-populated with a current list of solicitors. Also on the new prospect form I have the link_to new_solicitor_path, to add a new solicitor if one is needed. After the used is taken to the new solicitor form, fills it out and hits the submit button he is taken back to the new prospect form. This all works.

What I would like to have happen is when a user is redirect back to the new prospect form all the information he had entered prier to clicking the "new solicitor" link is preserved. Right now when a user finishes creating a new solicitor and is redirected back to the prospect form, all the form fields are blank again.

Update I will try and be clearer.

A user clinks a link to create a new prospect

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

this links to a the new action which renders a form partial

<%=render 'form'%>

in the form is a select box that is pre-populated with the current solicitors and has a link to add a new solicitor.

<%= 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 the new solicitors link they are taken to the new solicitors form. In the controller for solicitors

def create
      @[email protected](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 the new prospect form but all the data the user entered before clicking the new solicitors link is lost. Thanks to Marc's comments I think I understand that using the redirect_to is causing the problem. I am just not sure what the solution is.

like image 913
DomWolfe Avatar asked Feb 06 '12 22:02

DomWolfe


1 Answers

Instead of redirecting, just render the :new action. The form items should remain.

# ...
else
  flash[:error] = 'Could not save Solicitor.'
  # should include .fieldWithErrors formatting in the view
  render :action => 'new'
end
like image 58
Eric Avatar answered Nov 10 '22 13:11

Eric