代码之家  ›  专栏  ›  技术社区  ›  frillybob

RoR InvalidAuthentityToken与活动存储直接上载

  •  0
  • frillybob  · 技术社区  · 5 年前

    如果没有 ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken): 错误。

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

    application_controller.rb skip_before_action :verify_authenticity_token

    只有当我 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 %>

    0 回复  |  直到 5 年前
        1
  •  1
  •   kasperite    5 年前

    看起来ajax submit没有获取令牌。您可以尝试以下任一方法:

    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

    推荐文章