title: Rails form_for how to update or create new?
body: <p>I have a form_for that looks like this:</p>
  
  <pre><code>&lt;%= form_for (@product||(Product.new([some params])), :remote =&gt; true, :as =&gt; :product_data, :url =&gt; {:controller =&gt; :products, action: :update}, :html =&gt; {:class =&gt; 'form-horizontal'} do |f| %&gt;
  ...
  &lt;%= f.submit %&gt;
  </code></pre>
  
  <p>In my controller I have 'update' action.</p>
  
  <p>When the @product already exists the form is working just fine. It sends to the 'update' action in the controller.
  When the @product doesn't exist, it is creating a new temp product instance but submitting the form is not saving it. As a result, at page refresh the information is lost.</p>
  
  <p>How can I both 'update' if @product is present and create new + update it with the form if @product is not present using the same form?</p>
  
  <p><strong>SOLVED:</strong> </p>
  
  <p>In the update action of the products controller I added:</p>
  
  <pre><code>product = Product.where(some params).first rescue nil
  if product.blank?
  product = Product.create(some values)
  end
  </code></pre>