title: Rails 4 how to keep form data upon redirect
body: <p>I have a view, where I list data, and allow users to create new records (on a partial). The problem is, the form lives under the index action, so I lose the form's data if the user input is invalid upon redirect.</p>
  <p>I want to keep the data, so the user doesn't have to type it all over again.</p>
  <p>Controller:</p>
  <pre><code>def index  
      @instance = Instance.new
  end 
  
  def create  
      w = current_user.workspaces.find_by_id(params[:&quot;...&quot;]
   if w.present?
    @instance =  current_user.workspaces.find_by_id( [:&quot;...&quot;]).instances.create(...) 
    [:info] = &quot;Please check your email to activate your account.&quot;
        redirect_to root_url          
      else
        flash[:info] = &quot;Please check your data&quot; 
        redirect_to root_url
      end 
        
  end
  </code></pre>
  <p>index.html.haml</p>
  <pre><code>= render :partial =&gt; 'partial1'
  </code></pre>
  <p>partial1.html.haml</p>
  <pre><code>= form_for @instance , url: {action: &quot;create&quot;}  do |f|
  </code></pre>
  <p>I know that 'render' keeps the form's data, but doing:</p>
  <pre><code>def create  
      if something   
         ...
      else
         flash[:info] = &quot;Please check your data&quot; 
         render :index
      end 
            
  end
  </code></pre>
  <p>I get the error</p>
  <blockquote>
  <p>First argument in form cannot contain nil or be empty</p>
  <p>= form_for @instance , url: {action: &quot;create&quot;}  do |f|</p>
  </blockquote>
  <p>How can I fix it?
  I don't want to initialize @instance on the create action, like this</p>
  <pre><code>def create  
   ...
   flash[:info] = &quot;Please check your data&quot; 
   @instance = Instance.create
   render :index
  </code></pre>
  <p>because I would be repeating code, which is already written on the index action.</p>
  <hr />
  <p>to wrap it up:</p>
  <p>I have a complex index view which has a form partial, with a complex controller.
  I Can't render :index on the create action, because I would have to load all the index's instances variables.
  I can't create an @instance variable if the data is incorrect, since I have a nested form.</p>