RoR InvalidAuthenticityToken与Active Storage直接上传

时间:2019-02-13 21:50:55

标签: ruby-on-rails rails-activestorage

我正在尝试使用Rails主动存储直接上传到Amazon s3存储桶。但是,当存储介质是本地磁盘时,让我们将其视为直接上传,因为我也无法使它正常工作。

如果没有ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken): 错误,我将无法执行此操作。

我完全遵循了本指南:https://guides.rubyonrails.org/active_storage_overview.html#direct-uploads

我什至可以在application_controller.rb skip_before_action :verify_authenticity_token

中使用这一行代码来禁用csrf应用程序

仅当我的表单中有direct_upload: true时,这种情况才会发生。

这是我的表格:

<%= form_with url: messages_path do |form| %> <%= form.label 'Upload' %> <%= form.file_field :audio_message, class: 'form-control', direct_upload: true, :required => true%> <%= form.submit 'Upload file', class: 'form-control' %> <% end %>

1 个答案:

答案 0 :(得分:1)

看起来像ajax提交未领取令牌。您可以尝试以下任一方法:

  1. 告诉表单嵌入令牌<%= form_with url: messages_path, authenticity_token: true...%>

  2. 在配置中添加config.action_view.embed_authenticity_token_in_remote_forms = true

看看他们是否对您有帮助。我的猜测完全来自此:https://edgeapi.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_with