1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

ActionController::InvalidAuthenticityTokenの対処方法

Posted at

環境

Rails 6.1.6.1
ruby 2.6.8
OS WSL2(Ubuntu 20.04.2 LTS)

APIを使用したPOSTの際にエラーが起こる

エラー文

image.png

ActionController::InvalidAuthenticityToken in Users::XXXXController#create
ActionController::InvalidAuthenticityToken

原因

CSRF対策のトークンが無いことが原因のようです。
※CSRF:クロスサイトリクエストフォージェリ

対処方法

protect_from_forgeryをcontrollerに追記することで解決

class Users::XXXXController < ApplicationController
  protect_from_forgery #追加
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?