Rails 5: Remote form failing
<p>I have a form for create <strong>products</strong> in my rails app:</p>

<pre><code>= form_for product, url: url, method: :post, html: { class: 'product-form' }, remote: true do |f|

  .row
    .col-xs-12.col-sm-6
      .form-group.required
        = f.label :name
        = f.text_field :name, autofocus: true, class: 'form-control'
    .col-xs-12.col-sm-6
      .form-group.required
        = f.label :price
        .input-group
          .input-group-addon $
          = f.number_field :price, class: 'form-control'

  .row
    .col-xs-12
      .form-group
        = f.label :description
        = f.text_area :description, class: 'form-control'

  .row
    .col-xs-12
      .form-group
        = label_tag :images, 'Images'
        = f.file_field :images, multiple: true, class: 'form-control'

  .row
    .col-xs-12.text-right
      = f.submit "Cancel", class: 'btn btn-default', data: { dismiss: "modal" }
      = f.submit "Save",  class: 'btn btn-primary'
</code></pre>

<p>It works fine on my development environment. But when running on heroku (production), it fails when clicking on "Save" (<strong>note that I am using Devise and the login/signup works fine</strong>), with this error:</p>

<blockquote>
  <p>2016-10-14T11:55:02.825234+00:00 app[web.1]: W, [2016-10-14T11:55:02.825168 #3]  WARN -- Can't verify CSRF token authenticity.</p>
  
  <p>2016-10-14T11:55:02.825602+00:00 app[web.1]: I,
  [2016-10-14T11:55:02.825555 #3]  INFO -- : Completed 422 Unprocessable
  Entity in 1ms (ActiveRecord: 0.0ms)</p>
  
  <p>2016-10-14T11:55:02.826511+00:00 app[web.1]: F,
  [2016-10-14T11:55:02.826471 #3] FATAL -- :
  ActionController::InvalidAuthenticityToken
  (ActionController::InvalidAuthenticityToken):</p>
</blockquote>

<p><strong>The thing is when I don't select an image, it works fine. So I think the problem might be with the image uploader</strong>:</p>

<p><strong>models/product.rb</strong></p>

<pre><code>class Product
  mount_uploaders :images, ProductImageUploader
  # more stuff...
end
</code></pre>

<p><strong>uploaders/product_image_uploader.rb</strong></p>

<pre><code>class ProductImageUploader &lt; CarrierWave::Uploader::Base
  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end
end
</code></pre>

<p><strong>initializers/carrier_wave.rb</strong></p>

<pre><code>if Rails.env.development? || Rails.env.test?
  CarrierWave.configure do |config|
    config.storage = :file
    config.enable_processing = false if Rails.env.test?
  end
else
  CarrierWave.configure do |config|
    config.fog_provider = 'fog/aws'                        # required
    config.fog_credentials = {
      provider:              'AWS',
      aws_access_key_id:     ENV["AWS_KEY_ID"],
      aws_secret_access_key: ENV["AWS_KEY_SECRET"],
      region:                'us-west-2',                  # optional, defaults to 'us-east-1'
    }

    config.fog_directory = ENV['AWS_BUCKET']
    config.fog_public = true

    config.cache_dir = "#{Rails.root}/tmp/uploads"
  end
end
</code></pre>

<p>Another thing is that I tried to to a simple storage: :file in production, avoiding the S3 upload stuff, and it fails too.</p>

<p>Any idea?</p>

<p><strong>UPDATE!</strong></p>

<p>I added the <strong>authenticity_token: true</strong> in the form and it doesn't raise the exception I mentioned above. However, something weird is happening:</p>

<ul>
<li>When I don't select an image from the form, the POST is done remote:</li>
</ul>

<blockquote>
  <p>Processing by ProductsController#create as JS</p>
</blockquote>

<ul>
<li>When I select an image in the form, the POST is not done remote:</li>
</ul>

<blockquote>
  <p>Processing by ProductsController#create as HTML</p>
</blockquote>

<p><strong>UPDATE 2!</strong></p>

<p>This is the generated html for the form tag:</p>

<pre><code>&lt;form class="product-form" id="new_product" enctype="multipart/form-data" action="/seller/products" accept-charset="UTF-8" data-remote="true" method="post"&gt;
  &lt;input name="utf8" type="hidden" value="✓"&gt;
  &lt;input type="hidden" name="authenticity_token" value="SOME STUFF"&gt;     &lt;/form&gt;
</code></pre>