Rails 5:远程表单失败

时间:2016-10-14 12:08:15

标签: ruby-on-rails heroku amazon-s3 carrierwave ruby-on-rails-5

我在rails应用中有一个用于创建产品的表单:

= 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'

它在我的开发环境中运行良好。但是当在heroku(生产)上运行时,单击" Save" (请注意我正在使用Devise并且登录/注册正常),出现此错误:

  

2016-10-14T11:55:02.825234 + 00:00 app [web.1]:W,[2016-10-14T11:55:02.825168#3]警告 - 无法验证CSRF令牌的真实性

     

2016-10-14T11:55:02.825602 + 00:00 app [web.1]:我,   [2016-10-14T11:55:02.825555#3]信息 - :已完成422无法处理   1ms内的实体(ActiveRecord:0.0ms)

     

2016-10-14T11:55:02.826511 + 00:00 app [web.1]:F,   [2016-10-14T11:55:02.826471#3]致命 - :   ActionController的:: InvalidAuthenticityToken   (ActionController的:: InvalidAuthenticityToken):

事情是,当我不选择图像时,它可以正常工作。所以我认为问题可能出在图像上传器上

模型/ product.rb

class Product
  mount_uploaders :images, ProductImageUploader
  # more stuff...
end

上传/ product_image_uploader.rb

class ProductImageUploader < CarrierWave::Uploader::Base
  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end
end

初始化/ carrier_wave.rb

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

另一件事是我尝试了一个简单的存储::生产中的文件,避免了S3上传的东西,它也失败了。

有什么想法吗?

更新<!/强>

我在表单中添加了 authenticity_token:true ,它没有引发我上面提到的异常。但是,发生了一些奇怪的事情:

  • 当我不从表单中选择图像时,POST是远程完成的:
  

ProductsController处理#create as JS

  • 当我在表单中选择图像时,POST不会远程完成:
  

ProductsController处理#create as HTML

更新2!

这是表单标记生成的html:

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

1 个答案:

答案 0 :(得分:3)

在表单声明中,在remote: true之后添加此authenticity_token: true,如:

form_for product, url: url, method: :post, html: { class: 'product-form' }, remote: true, authenticity_token: true do |f|