レコードを更新 (または作成) しようとすると、無効な CSRF トークン エラーが発生します。Elixir v1.0.3、Erlang/OTP 17 [erts-6.3]、および Phoenix v0.8.0 を使用しています (Phoenix のバージョンを確認する方法がわかりません)。私は主に Phoenix ガイドと Elixir Dose Jobsite Example リソースに従って Web アプリを作成しています。ただし、html フォームから情報を投稿しようとすると、Invalid CSRF token エラーが発生します。エラーのアドバイスに従い、'x-csrf-token': csrf_token をアクションに追加しました。
edit.html.eex:
<h2>Edit Directory</h2>
<form class="form-horizontal" action="<%= directory_path @conn, :update, @directory.id, 'x-csrf-token': @csrf_token %>" method="post">
<div class="form-group">
<label for="directory" class="col-sm-2 control-label">Directory</label>
<div class="col-sm-10">
<input type="hidden" name="_method" value="PATCH">
<input type="text" class="form-control" value="<%= @directory.directory %>" name="directory" placeholder="Directory" required="required">
</div>
</div>
...
しかし、次のエラーが表示されます。
[error] #PID<0.579.0> running Ainur.Endpoint terminated
Server: localhost:4000 (http)
Request: POST /config/directories/2?x-csrf-token=
** (exit) an exception was raised:
** (Plug.CSRFProtection.InvalidCSRFTokenError) Invalid CSRF (Cross Site Forgery Protection) token. Make sure that all your non-HEAD and non-GET requests include the csrf_token as part of form params or as a value in your request's headers with the key 'x-csrf-token'
(plug) lib/plug/csrf_protection.ex:54: Plug.CSRFProtection.call/2
(ainur) web/router.ex:4: Ainur.Router.browser/2
(ainur) lib/phoenix/router.ex:2: Ainur.Router.call/2
(plug) lib/plug/debugger.ex:104: Plug.Debugger.wrap/3
(phoenix) lib/phoenix/endpoint/error_handler.ex:43: Phoenix.Endpoint.ErrorHandler.wrap/3
(ainur) lib/ainur/endpoint.ex:1: Ainur.Endpoint.phoenix_endpoint_pipeline/2
(plug) lib/plug/debugger.ex:104: Plug.Debugger.wrap/3
(phoenix) lib/phoenix/endpoint/error_handler.ex:43: Phoenix.Endpoint.ErrorHandler.wrap/3
私が知る限り (Elixir、Phoenix、および HTML は初めてです)、「アクション」は基本的にパスであり、そこに配置したパラメーターはアプリケーションに戻る方法を見つけます。実際、x-csrf-token = "" がルーターに戻されていることがわかりました。そのため、@csrf_token は正しくないはずです。csrf_token がどこから来たのか正確にはわからないので、それを参照する方法がわかりません (または、これを完全に間違っている可能性があります)。
どんなアイデアでも大歓迎です。