尝试从 github 获取 emails_controller.rb 吗?
https://github.com/thoughtbot/griddler
“在 Rails API 上使用 verify_authenticity_token 修复 ArgumentError”
我已经添加protect_from_forgery with: :null_session
我认为 null_session 应该在 API 控制器中使用,而不用于会话。它在请求期间提供一个空会话。
https://api.rubyonrails.org/classes/ActionController/RequestForgeryProtection/ClassMethods.html
在控制器文件夹 griddler 中添加来自 Griddler github 的新文件,或尝试下面的代码。
/controllers/griddler/emails_controller.rb
class Griddler::EmailsController < ActionController::Base
skip_before_action :verify_authenticity_token, raise: false
protect_from_forgery with: :null_session
def create
normalized_params.each do |p|
process_email email_class.new(p)
end
head :ok
end
private
delegate :processor_class, :email_class, :processor_method, :email_service, to: :griddler_configuration
private :processor_class, :email_class, :processor_method, :email_service
def normalized_params
Array.wrap(email_service.normalize_params(params))
end
def process_email(email)
processor_class.new(email).public_send(processor_method)
end
def griddler_configuration
Griddler.configuration
end
end
不完全确定整个解决方案,但希望它有助于获得更多方向。