title: Error: Validation failed: Images imageable must exist , rails-5.0 , paperclip-5
body: <p>While i was trying to submit the form, following error occured: <code>Validation failed: Images imageable must exist</code> and render the same <code>new.html.erb</code> view.</p>
  
  <p>If i comment the <code>file field</code> in <code>new.html.erb</code>. Product is being created successfully.</p>
  
  <p><strong>ProductsController:</strong></p>
  
  <hr>
  
  <pre><code>def new
      @product = Product.new
  end
  
  def create
      @product = Product.create!(product_params)
  
      if @product.save
          redirect_to products_path, notice: "Product Created Successfully"
      else
          render "new"
      end
  end
  def product_params
          params.require(:product).permit(:name, :quantity, :price, images_attributes: [:id, :photo, :_destroy])
  end
  </code></pre>
  
  <p><strong>new.html.erb:</strong></p>
  
  <hr>
  
  <pre><code>&lt;%= nested_form_for @product, html: { multipart: true } do |f|%&gt;
  
      &lt;h2&gt;New&lt;/h2&gt;
  
      &lt;P&gt; &lt;%= f.label :name %&gt; &lt;%= f.text_field :name %&gt; &lt;/P&gt;
      &lt;P&gt; &lt;%= f.label :quantity %&gt; &lt;%= f.text_field :quantity %&gt; &lt;/P&gt;
      &lt;P&gt; &lt;%= f.label :price %&gt; &lt;%= f.text_field :price %&gt; &lt;/P&gt;
    &lt;%= f.fields_for :images do |p| %&gt;
      &lt;p&gt; &lt;%= p.label :photo %&gt; &lt;%= p.file_field :photo %&gt; &lt;/p&gt;
      &lt;%= p.link_to_remove "Remove Image" %&gt;
    &lt;% end %&gt;
    &lt;%= f.link_to_add "Add Image", :images %&gt;
  
    &lt;%= f.submit "Add Product" %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p><strong>20160725102038_add_image_columns_to_imageable.rb:</strong></p>
  
  <hr>
  
  <pre><code>class AddImageColumnsToImageable &lt; ActiveRecord::Migration[5.0]
  
    def up
      add_attachment :images, :photo
    end
  
    def down
      remove_attachment :images, :photo
    end
  
  end
  </code></pre>
  
  <p><strong>Model:product.rb</strong></p>
  
  <hr>
  
  <pre><code>class Product &lt; ApplicationRecord
      has_one :variant
    has_many  :images, as: :imageable,  dependent: :destroy
  
    accepts_nested_attributes_for :images, allow_destroy: true
  end
  </code></pre>
  
  <p><strong>Model:image.rb</strong></p>
  
  <hr>
  
  <pre><code>class Image &lt; ApplicationRecord
    belongs_to :imageable, polymorphic: true
  
    has_attached_file :photo, styles: { medium: "300x300&gt;", thumb: "100x100&gt;" }, default_url: "/images/:style/missing.png"
    validates_attachment :photo, content_type: { content_type: ["image/jpg", "image/jpeg", "image/png", "image/gif"] }
  
  end
  </code></pre>