为什么正确路由的 POST 请求会在 Rails 和 shopify_app gem 中抛出 "ActionController::InvalidAuthenticityToken" 错误

Why does a POST request, properly routed, throw an "ActionController::InvalidAuthenticityToken" error in Rails and the shopify_app gem

在我的应用程序中向 /locations/1/submit-to-shopify 发出 post 请求时出现以下错误:

ActionController::InvalidAuthenticityToken in LocationsController#submitshopify

您可以在下面我的 routes.rb 文件中看到 post 路线。

  root 'home#index'

  controller :sessions do
    get 'login' => :new, :as => :login
    post 'login' => :create, :as => :authenticate
    get 'auth/shopify/callback' => :callback
    get 'logout' => :destroy, :as => :logout
    get 'locations/:id' => 'locations#index'
    post 'locations/:id/submit-to-shopify' => 'locations#submitshopify'
  end

所有其他请求工作正常。这是我的位置控制器:

class LocationsController < AuthenticatedController
  def index
    @location_id = params[:id]
    @location = Location.find(@location_id)
  end
  def submitshopify
    @location_id = params[:id]
    @location = Location.find(@location_id)
    @product_handle = params[:product_handle]
    @product = ShopifyAPI::Product.find_by handle: @product_handle
  end
  def new
  end
  def create
  end
  def show
  end
  def edit
  end
  def update
  end
  def destroy
    @location_id = params[:id]
    @location = Location.find(@location_id)
    @destroy_status = @location.destroy
  end
end

如果有任何影响,我正在使用 'shopify_app' gem,其中包括 'shopify_api'。我遵循了为这两个模块列出的所有说明,并在所有其他页面/控制器中成功通过 Shopify 进行了身份验证。

您是否使用助手来创建您的提交表单?

还是您手动创建的?如果手动创建,您是否添加了

<%= csrf_meta_tags %> 

到你的表格?

检查您的 firebug 表单末尾是否有身份验证令牌。即使查看源代码也应该向您展示。

我们应该摒弃任何人试图通过像 curl 这样的 http 客户端使用您的表单发送垃圾邮件而实际上并没有在您的站点上(跨站点请求伪造)